Different movement handling options

Hello,

I’m a novice and I’ve recently been researching character movement speed. So far I encountered two distinct speed handling options and I would like to know your thoughts on what is best.

Presume you want to achieve a sprint speed of 2000 and a walking speed of 1000.

  1. First method: edit MaxWalkSpeed

Basically set in the constructor MaxWalkSpeed to the walk speed of 1000 and a bool stating that by default we are in walk mode:

	sprinting = false;
	characterMovementComponent = GetCharacterMovement();
	characterMovementComponent->MaxWalkSpeed = 1000.0f;

then proceed to update the bool and the MaxWalkSpeed in our Sprint function:

void AMyFPCharacter::Sprint() {
	if (!sprinting) {
		UE_LOG(LogTemp, Warning, TEXT("sprint ON"));
		characterMovementComponent->MaxWalkSpeed = 2000.0f;
	} else {
		UE_LOG(LogTemp, Warning, TEXT("sprint OFF"));
		characterMovementComponent->MaxWalkSpeed = 1000.0f;
	}
	sprinting = !sprinting;
}

Movement input as follows:

void AMyFPCharacter::HorizontalMove(float value) {
	if (value) {
		AddMovementInput(GetActorRightVector(), value);		
	}
}

void AMyFPCharacter::VerticalMove(float value) {
	if (value) {
		AddMovementInput(GetActorForwardVector(), value);
	}
}
  1. Second method: multiply value to AddMovementInput() parameters.

In the constructor set MaxWalkSpeed to 2000 and defaul walk speed with the bool:

	sprinting = false;
	characterMovementComponent = GetCharacterMovement();
	characterMovementComponent->MaxWalkSpeed = 2000.0f;

In Sprint simply change the value of a random variable:

void AMyFPCharacter::Sprint() {
	if (!sprinting) {
		UE_LOG(LogTemp, Warning, TEXT("sprint ON"));
		speedMultiplier = 1.0f;
	} else {
		UE_LOG(LogTemp, Warning, TEXT("sprint OFF"));
		speedMultiplier = 0.5f;
	}
	sprinting = !sprinting;
}

Use this variable in Movement Input:

void AMyFPCharacter::HorizontalMove(float value) {
	if (value) {
		AddMovementInput(GetActorRightVector(), value * speedMultiplier);		
	}
}

void AMyFPCharacter::VerticalMove(float value) {
	if (value) {
		AddMovementInput(GetActorForwardVector(), value * speedMultiplier);
	}
}

The only difference I noticed is that when first accelerating to the top speed, the first method accelerates faster and the second method slightly slower, although difference is minimal and it may even be my imagination.

In any case, end result is the same, so my question is, which method is better/most optimal? Is there any reason why you would pick one method over the other?

Thanks!