I’m trying to make it so that the character in my game is able to sprint by holding the left shift key. However, currently when I hold down the left shift key my character’s speed doesn’t change at all.
I’ve added the action mapping to the project setting for the left shift key.
I’ve mapped the action on both IE_Pressed and IE_Released in my SetupPlayerInputComponent function:
InputComponent->BindAction("Sprint", EInputEvent::IE_Pressed, this, &APlayerCharacter::OnStartSprint);
InputComponent->BindAction("Sprint", EInputEvent::IE_Released, this, &APlayerCharacter::OnStopSprint);
The functions being bound for the input event are:
void APlayerCharacter::OnStartSprint()
{
bPressedSprint = true;
}
void APlayerCharacter::OnStopSprint()
{
bPressedSprint = false;
}
And my functions for handling movement are set up like so:
void APlayerCharacter::MoveFrontBack(float Value)
{
if (Controller && Value != 0.0f)
{
FRotator Rotation = GetActorRotation();
if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling())
{
Rotation.Pitch = 0.0f;
}
FVector const Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
if (bPressedSprint) Value *= SprintModifier;
AddMovementInput(Direction, Value);
}
}
void APlayerCharacter::MoveLeftRight(float Value)
{
if (Controller && Value != 0.0f)
{
FRotator const Rotation = GetActorRotation();
FVector const Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
if (bPressedSprint) Value *= SprintModifier;
AddMovementInput(Direction, Value);
}
}
The SprintModifier and bPressedSprint variables are also declared in my header file:
private:
bool bPressedSprint = false;
float SprintModifier = 2.0f;
Am I missing something obvious here as to why my character’s speed does not increase when holding down the left shift key?