Well, I’ve faced with a problem… I don’t have a controller to control the speed of my character, and I saw a comments that some people have faced with the same problem too. Here is my decision for this!
So I decided to add a functionality to run and walk with the help of my keyboard, and with the help of shift button of course.
First of all we need to add a button in Input settings to manage the speed of our character. I decided to run with pressed “Left Shift” and just walk in normal state without pressing any extra buttons.
Great! After that we need to Bind this button with our PlayerInputComponent in SchooterCharacter.cpp
So let’s figure out what the logic we need. It’s simple!
We need to hold Left Shift all the time the character running and when the button released we need to start walking. Ok, then we need 2 functions when the button pressed and when it released. Let’s create 2 functions for that!
In your ShooterCharacter.h in private sections create two functions. I have called them:
void IncreaseSpeed();
void DecreaseSpeed();
And we need some variable to understand if the button was pressed for some moment or not. Create a bool variable. I have called it
bool bIsShiftPressed = false;
Let’s bind our functions!
Bind this functions with IE_Repeat
and IE_Release
Input events. I use IE_Repeat
because it will be calling that function every time the button is pressed and not just once like it would be with IE_Pressed
PlayerInputComponent->BindAction(TEXT("Speed"), IE_Repeat, this, &AShooterCharacter::IncreaseSpeed);
PlayerInputComponent->BindAction(TEXT("Speed"), IE_Released, this, &AShooterCharacter::DecreaseSpeed);
After that create that functions. And when the shift is pressed we need to set bIsShiftPressed
to true
and when it released set it to false
:
void AShooterCharacter::IncreaseSpeed()
{
UE_LOG(LogTemp, Warning, TEXT("Shift pressed"));
bIsShiftPressed = true;
}
void AShooterCharacter::DecreaseSpeed()
{
UE_LOG(LogTemp, Warning, TEXT("Shift released"));
bIsShiftPressed = false;
}
Great! The last thing we need to do is to modify
void AShooterCharacter::MoveForward(float AxisValue)
and
void AShooterCharacter::MoveRight(float AxisValue)
So, when this shift is not pressed and we are moving we have to divide our speed 350 / 150 = 2.3. Right? NO. The thing is that our maximum speed from ABP_ShooterCharacter
is not 350 but 600(in my case). You can check yours by adding “VisLog Text” function if your blueprint, press play and check console log:
So, as my speed is 600, than to make 350 we need to devide it by 1.72(600 / 350 = 1.72) and for 150 we need to devide it by 4
So in
MoveForward
and MoveRight
functions I have made smth like this
void AShooterCharacter::MoveForward(float AxisValue)
{
AddMovementInput(GetActorForwardVector(), AxisValue / (bIsShiftPressed ? 1.72 : 4));
}
void AShooterCharacter::MoveRight(float AxisValue)
{
AddMovementInput(GetActorRightVector(), AxisValue / (bIsShiftPressed ? 1.72 : 4));
}
I divided AxisValue
by 1.72 or 4 depending by bIsShiftPressed
true
or false
.
That’s all! Press play and enjoy your walking animation!)