What is the best way to change player movement speed (multiplayer)

Hello, i have two float variables SprintSpeed and WalkSpeed. When the player clicks shift the movement speed has to change to SprintSpeed and when the player clicks shift again it changes to WalkSpeed again. My question is: What is the best way to change the speed ?
My idea is just to use a RPC and it works:

// .h file
UFUNCTION(Server, Reliable)
void ServerToggleMovementMode();

UFUNCTION(NetMulticast, Reliable)
void ClientToggleMovementMode();

// .cpp file
void AMyCharacter::ServerToggleMovementMode_Implementation()
{
	ClientToggleMovementMode();
}

void AMyCharacter::ClientToggleMovementMode_Implementation()
{
	if(GetCharacterMovement()->MaxWalkSpeed == WalkSpeed)
	{
		GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
	}
	else if(GetCharacterMovement()->MaxWalkSpeed == SprintSpeed)
	{
		GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
	}
}

But before i continue i want to know if there is a better way of doing this. Thank you !

Input β†’ Validate you can [true] β†’ Change Speed β†’ Srv Change Speed

Srv Change Speed (run on server) β†’ Validate you can [true] β†’ Change Speed


Always implement on client, then RPC server to attempt.

  • This will aid in client responsiveness
  • Server is Authority, so it will correct if its attempt fails.

You do not need to multicast CMC. 99.9999% is replicated internally.

1 Like

Thank you i think i understood what i have to do.

Here’s a simple Blueprint version of it using Rep_Notify enumerator (CharMoveSpeed).

When you Set/Change a Rep_Notify variables value its OnRep Function is executed. I take advantage of this on the client to force calling the function by changing the value locally.

I then call the server to run the flow logic to make its own determination.

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