Download

Problem with MovementComponent in multiplayer project

Hi there!

I am new at multiplayer devolopment and i having a problem with my project.

When i try to change MaxWalkSpeed, the actor speed changes, but everything around moves in jerks, as if fps drawdown begins.

Maybe someone knows where my problem is?

PlayerCharacter.h

UCLASS()
class KILLTHEM_API AKT_PlayerCharacter : public ACharacter
{
	GENERATED_BODY()

public:

	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	UFUNCTION(Server, Reliable)
		void Sprint();
	UFUNCTION(Server, Reliable)
		void Crouching();
}

PlayerCharacter.cpp

void AKT_PlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AKT_PlayerCharacter::Sprint);
	PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AKT_PlayerCharacter::Crouching);

	PlayerInputComponent->BindAxis("MoveForward", this, &AKT_PlayerCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AKT_PlayerCharacter::MoveRight);

	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}


void AKT_PlayerCharacter::Sprint_Implementation()
{
	if (IsSprinted)
	{
		GetCharacterMovement()->MaxWalkSpeed = 600;
		IsSprinted = false;
	}
	else
	{
		GetCharacterMovement()->MaxWalkSpeed = 900;
		IsSprinted = true;
	}
}

Fine, I’ll do it myself)

When working with multiplayer, it is not enough to change the variable on the server or client, you need to change here and there.

PlayerCharacter.h

UCLASS()
class KILLTHEM_API AKT_PlayerCharacter : public ACharacter
{
	GENERATED_BODY()

public:

	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	UFUNCTION()
		void Sprint();
	UFUNCTION(Server, Reliable)
		void SprintOnServer(float InSpeed);
	UFUNCTION(Server, Reliable)
		void Crouching();
}

PlayerCharacter.cpp


void AKT_PlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AKT_PlayerCharacter::Sprint);
	PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AKT_PlayerCharacter::Crouching);

	PlayerInputComponent->BindAxis("MoveForward", this, &AKT_PlayerCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AKT_PlayerCharacter::MoveRight);

	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}


void AKT_PlayerCharacter::Sprint()
{
	if (IsSprinted)
	{
		GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
		SprintOnServer(WalkSpeed);
		IsSprinted = false;
	}
	else
	{
		GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
		SprintOnServer(SprintSpeed);
		IsSprinted = true;
	}
}


void AKT_PlayerCharacter::SprintOnServer_Implementation(float InSpeed)
{
	GetCharacterMovement()->MaxWalkSpeed = InSpeed;
}

I hope this helps someone