For someone who doesn't have a controller

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;
}

functions

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!)

5 Likes

Thank you so much for sharing this!

1 Like

thank you but can we do just in Blueprint only . does anyone know ?

OK. I did some research and watched some tutorials from Unreal Learn Animation Basics…it is accessible from Epic Launcher or web site

just adding shift left key to Action Binding and call this directly from BP_Character event graph and get a reference from CharacterMovement component by just click on it and dragging.

The we get Set max walk speed from CharacterMovement node and set it double amount 1200 and when released back to original value 600 (we could use a multiply modifier too to avoid magic numbers)

Then tie the executions to InputAction Sprint or Speed whatever it is called in the Action Binding.

It looks like it is speeding up nicely;

3 Likes

Yes, I have found another trick, you can check your movement speed with selecting
BP_ShooterCharacter and in components tab select CharacterMovement component and in details panel there will be a tap called Character Movement: Walking and here you can set you max speed

1 Like

Thank you for sharing this! I wanted to make a solution like this in c++.
But I found another way and it’s easier, using the BP, and you can do it in seconds …
But I like coding more, so thanks you again!