Add Sprinting to the UE5 Third Person Character in C++
Let's quickly add a sprint button to the third person character using Unreal's Enhanced Input System.

Game Engine
Unreal Engine 5.5.3
IDE
Rider 2024.3.5
Project Name
MyProject
OS
macOS Sequoia 15.3
To move around the world faster I wanted to add a sprint mechanic to the third person character allowing more freedom of movement. Using Unreal's Enhanced Input System made it pretty trivial and seemless.
First, add a new a Input Action. I'm leveraging the already existing Third Person character so I followed the previous laid pattern. I called the new action IA_Sprint

Next, configure the input action. I added a Down
trigger so the player will need to hold the button to sprint, I left just about everything else as the default settings.

With the input action created we can now add it to IMC_Default
Input Mapping Context. I added two control bindings, Left Shift
and Gamepad Left Thumbstick Button
.

Since the third person template already had the context mapping set, we can just follow the pattern that was already established. Inside the header file I added a new UInputAction
and new Sprint
function.
TP_ThirdPersonCharacter.h
...
/** Sprint Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* SprintAction;
public:
ATP_ThirdPersonCharacter();
protected:
...
/** Called for sprinting input */
void Sprint(const FInputActionValue& Value);
Then in the .cpp
file inside the SetupPlayerInputComponent
function we can bind our Sprint
inputs with EnhancedInputComponent->BindAction
. We'll two action bindings, one for each event, the Triggered
event and the Completed
event.
TP_ThirdPersonCharacter.h.cpp
void ATP_ThirdPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
...
// Sprinting
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Triggered, this, &ATP_ThirdPersonCharacter::Sprint);
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Completed, this, &ATP_ThirdPersonCharacter::Sprint);
}
The Sprint
function that the action is referencing is very simple and can likely be improved with variables or etc, but for the example I kept it simple. First, we want to know if the button is being held down, we only need a boolean
so we use Value.Get<bool>();
, our Value
is a FInputActionValue
that is comes as the first argument in the binding. Once we get the value we can do a ternary to set the character movement component's MaxWalkSpeed
.
TP_ThirdPersonCharacter.h.cpp
void ATP_ThirdPersonCharacter::Sprint(const FInputActionValue& Value)
{
bool bIsSprinting = Value.Get<bool>();
GetCharacterMovement()->MaxWalkSpeed = bIsSprinting ? 1200.f : 500.f;
}
Additionally when the sprint button is held I wanted to speed up the run animation to indicate a faster running pace. To do this went inside the BS_MF_Unarmed_WalkRun
Blend Space 1D Blueprint and added an additional animation key with a rate scale of 1.5
and speed of 1200
. I had to also increase the Maximum Axis Value
to 1200
to account for new max speed.


With the new sprint button mechanic we can now run around the world and explore much quicker.

I hope this helped.

Harrison McGuire