[Multiplayer C++] Client is not sprinting

Im trying to make sprinting in my game. I want that the player can only sprint in forward direction ( if player is pressing the W and the shift key).But if the player presses the S key and he is still holding the shift key ,I want that the player stops sprinting.
This only works on the server side, on client side doesn’t work.

Header

UFUNCTION(Server, Reliable)
		void ServerSprint(bool isSprinting);
	UFUNCTION(NetMulticast, Reliable)
		void MulticastSprint(bool isSprinting);

	bool bIsHoldingSprintButton;

Forward Axis and Sprint Input Action

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

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

PlayerInputComponent->BindAction("SprintHold", EInputEvent::IE_Pressed, this, &APlayerCharacter::SprintHold);
	PlayerInputComponent->BindAction("SprintRelease", EInputEvent::IE_Released, this, &APlayerCharacter::SprintRelease);
}

Bindings

void APlayerCharacter::MoveForward(float value)
{
	forwardInput = value;

	if(value != 0.f)
	{
		const FRotator rotation = GetControlRotation();
		const FRotator yawRotation(0, rotation.Yaw, 0);
		const FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(direction, value);
	}
}

void APlayerCharacter::SprintHold()
{
	bIsHoldingSprintButton = true;
	if (bIsHoldingSprintButton)
	{
		ServerSprint(bIsHoldingSprintButton);
	}
}

void APlayerCharacter::SprintRelease()
{
	bIsHoldingSprintButton = false;
	ServerSprint(false);
}

void APlayerCharacter::ServerSprint_Implementation(bool isSprinting)
{
	MulticastSprint(isSprinting);
}

void APlayerCharacter::MulticastSprint_Implementation(bool isSprinting)
{
	if(isSprinting && forwardInput > 0.f)
	{
		GetCharacterMovement()->MaxWalkSpeed = 600.f;
	}
	else
	{
		GetCharacterMovement()->MaxWalkSpeed = 300.f;
	}
}

I tried to replicate the forwardInput variable but still doesn’t work

The bIsSprinting should be replicated and set on the serverSprint. If it is in the SprintHold action, it would be setting its value on the owning client only, which is why only the server works because the owning client is the server.

1 Like

First you don’t need to multicast the sprint, you know you’re sprinting, you let the server know and that’s it, other clients don’t need to know because the server already handles replicating your position.

You can do something like this.

void APlayerCharacter::MoveForward(float value)
{
	if (value != 0.f)
	{
		if(bIsSprinting && value < 0.f)
		{
			bIsSprinting = false;
			ServerSprint(false);
			//Set Speed = Normal
		}else if(!bIsSprinting && bIsHoldingSprintButton && value > 0.f)
		{
			bIsSprinting = true;
			ServerSprint(true);
			//Set Speed = Sprint
		}else if(bIsSprinting && !bIsHoldingSprintButton)
		{
			bIsSprinting = false;
			ServerSprint(false);
			//Set Speed = Normal
		}

		//input stuff..
	}
}

void APlayerCharacter::ServerSprint_Implementation(bool bValue)
{
	bIsSprinting = bValue;
	if(bValue)
	{
		//Set Speed = sprint
	}else
	{
		//Set Speed = Normal
	}
}

void APlayerCharacter::SprintHold()
{
	bIsHoldingSprintButton = true;
}

void APlayerCharacter::SprintRelease()
{
	bIsHoldingSprintButton = false;
}
1 Like

Thank you, it worked

1 Like

I tried to make like you said but it didn’t work. Thank you anyway

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.