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.

Unreal Engine 5 BS_MF_Unarmed_WalkRun Blend Space 1D editor screen Quinn as the Skeletal Mesh
BS_MF_Unarmed_WalkRun Blend Space 1D

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

Unreal Engine 5 adding a new Input Action in the editor
Adding 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.

Unreal Engine 5 IA_Sprint input action configuration
IA_Sprint Configurations

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.

Unreal Engine 5 IMC_Default Input Mapping Context with IA_Sprint added
IMC_Default with IA_Sprint

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.

Unreal Engine 5 BS_MF_Unarmed_WalkRun Blend Space 1D Blueprint with the MF_Run_Fwd animation highlighted
Drag in an additional MF_Run_Fwd animation to create a new fourth sample we can place at 1200
Unreal Engine 5 BS_MF_Unarmed_WalkRun Blend Space 1D Blueprint with the axis settings and the new 1200 key settings highlighted
Maximum Axis Value: 1200 | Speed: 1200 and Rate Scale: 1.5 (adjust to fit your needs)

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

Unreal Engine 5 third person character sprint button in action
Quinn Sprintin'

I hope this helped.

Comments (0)

Add a Comment

Sign in to comment or like.

Delete Comment

-