Character Movement provides a locomotion system for humanoid characters to traverse through your world. The Character Movement Component is an Actor Component that provides modes of movement for Character classes, including walking, falling, swimming, and flying. Additionally, the Character Movement Component features robust network gameplay integration and provides a framework to help developers create custom networked movement.
For additional documentation on Network Replication in Unreal Engine, refer to the Networking Overview.
In this How-To Gameplay Guide, you will create a Character class, script it's movement logic, Input, and create an Animation Blueprint which will help determine which state your Character is in, and then play the corresponding animation.
Creating a New Project
-
Begin by signing into the Epic Games Launcher and creating a New > Games > Blank > Project named CharacterMovement.
-
To animate your Character, you will require the Animation Starter Pack which is available to download for free from the Epic Games Marketplace. Click the Free button to download and add the Animation Starter Pack to your Epic Games Library.
-
From your Library, search for your Animation Starter Pack and click Add to Project, then from the Select the Project to Add the Asset to menu, search for and select your CharacterMovement project file, then click Add to Project.
-
Upon adding the Animation Starter Pack to your project, you will notice its presence in the Content Browser.
-
Begin by creating a New > Games > Blank > C++ project named CharacterMovement.
-
To animate your Character, You will require an Animation Starter Pack which is available to download for free from the Epic Games Marketplace. Click the Free button to download and add the Animation Starter Pack to your Epic Games Library.
-
From your Library, search for your Animation Starter Pack and click Add to Project, then from the Select the Project to Add the Asset to menu, search for and select your CharacterMovement project file, then click Add to Project.
-
Upon adding the animation starter pack to your project, you will notice its presence in the Content Browser.
Creating the Player Character
A Character class is required for your Player to control. A character is a pawn that comes with a Character Movement Component to provide a locomotion system for humanoid characters to traverse through your world. Follow the steps below to set up your character.
-
Click the Add/Import button to create a new Blueprint Character class named BP_PlayerCharacter.
-
Double-click your BP_PlayerCharacter to open its class defaults, then in the components tab select the Mesh(CharacterMesh0) Skeletal Mesh Component, and navigate to the Details panel.
-
In the Mesh category, select the drop down arrow adjacent to the Skeletal Mesh variable, and from the drop down menu select SK_Mannequin.
-
In the Details panel, navigate to the Transform category, and set the Mesh's Location and Rotation to (0,0,-90).
-
Your Character's Skeletal Mesh will now be oriented in the direction of the forward facing Arrow Component as shown in the image below.
-
Navigate to the Components tab to select your Mesh component, then click Add Component and in the drop down menu search for and select SpringArm, then name your Spring Arm Component "SpringArmComp".
-
Navigate to the Details panel, and in the Camera Settings category enable the Use Pawn Control Rotation.
-
In the Camera category, set the socket offset to (0,0,30) then navigate to the Transform category and set the SpringArmComp's Location to (0,0,50).
-
With your SpringArmComp selected, click the Add Component button, then in the drop down menu search for and select Camera to add a Camera Component named CameraComp.
-
In the Components tab, select the Character Movement Component, then navigate to the Details panel, and in the Character Movement (Rotation Settings) category, enable Use Controller Desired Rotation and Orient Rotation To Movement.
-
In the Character Movement:Walking category enable the Ignore Base Rotation variable.
-
In the Character Movement Nav Movement category, navigate to the Movement Capabilities and select the drop down arrow to reveal additional variable settings, navigate to the Can Crouch Boolean and click to enable it.
-
Compile and Save.
A Character class is required for your Player to control. A character is a pawn that comes with a Character Movement Component to provide a locomotion system for humanoid characters to traverse through your world. Follow the steps below to set up your character.
-
In the C++ Class Wizard, create a new Character class named PlayerCharacter, then navigate to the
PlayerCharacter.h
file and declare the following class definitions:protected: //Spring Arm Component to follow the camera behind the player UPROPERTY(VisibleAnywhere, BlueprintReadWrite) class USpringArmComponent* SpringArmComp; //Player follow camera UPROPERTY(VisibleAnywhere, BlueprintReadWrite) class UCameraComponent* CameraComp;
-
Navigate to the
PlayerCharactor.cpp
file and add the following class libraries:#include "Camera/CameraComponent.h" #include "GameFramework/SpringArmComponent.h" #include "GameFramework/CharacterMovementComponent.h"
-
In the APlayerCharactor::APlayerCharacter constructor method declare the following
// Instantiating your class Components SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp")); CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp")); //Set the location and rotation of the Character Mesh Transform GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f))); // Attaching your class Components to the default character's Skeletal Mesh Component. SpringArmComp->SetupAttachment(GetMesh()); CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName); //Setting class variables of the spring arm SpringArmComp->bUsePawnControlRotation = true; //Setting class variables of the Character movement component GetCharacterMovement()->bOrientRotationToMovement = true; GetCharacterMovement()->bUseControllerDesiredRotation = true; GetCharacterMovement()->bIgnoreBaseRotation = true;
-
Compile your code.
-
In the Editor, navigate to your C++ Classes folder, then right click on your PlayerCharacter class and in the C++ Class Actions drop down menu select Create Blueprint class based on PlayerCharacter. Name your Blueprint Bp_PlayerCharacter.
- Double-click your BP_PlayerCharacter to open it's class defaults, then in the Components tab select the Mesh(CharacterMesh0) Skeletal Mesh Component, and navigate to the Details panel.
-
In the Mesh category, select the drop down arrow adjacent to the Skeletal Mesh variable, and from the drop down menu select SK_Mannequin.
-
Your Character's Skeletal Mesh will appear oriented in the direction of the forward facing Arrow Component as shown in the image below.
-
Compile and Save.
Creating Input Key Mappings and Input Movement Events
You will need to set up some custom logic that will move your character when an Input key; such as W,A,S, or D is pressed.
- Navigate to Edit > Project Settings > Input, then in the Bindings Category click the + sign next to Action Mappings to add the following:
Action Mapping | Key Value |
---|---|
Jump | Space Bar |
Crouch | Left Ctrl |
Sprint | Left Shift |
- In the Bindings Category click the + sign next to Axis Mappings to add the following Axis Mappings.
Axis Mapping | Key Value | Scale |
---|---|---|
MoveForward | W | 1.0 |
S | -1.0 | |
MoveRight | D | 1.0 |
A | -1.0 | |
Turn | Mouse X | 1.0 |
LookUp | Mouse Y | -1.0 |
- In the Content Browser, double click your BP_PlayerCharacter to open it's class defaults, then right click on the Event Graph and in the drop down menu search for and select MoveForward.
- Drag off from the execution pin of your InputAxis MoveForward node and in the drop down menu search for and select for the Add Movement Input node, then connect the Axis Value pin of the InputAxis MoveForward node to the Scale Value pin of the Add Movement Input node.
- Right click on the Event Graph and in the context menu search for and select Get Control Rotation, then drag off the Rotator return value pin, and in the drop down menu search for and select Break Rotator.
- Drag off from the Break Rotator node's Z(Yaw) pin, and in the drop down menu search for and select Make Rotator, then drag off from the Make Rotator node's Rotator Return value pin and in the drop down menu search for and select Get Forward Vector.
- Drag off from the Get Forward Vector node's Vector Return Value pin and plug it into the Add Movement Input node's World Direction pin.
- Right-click on the graph again, and search for and select Move Right for your Input Axis Event.
- Drag off the execution pin of your InputAxis MoveRight node and in the drop down menu search for and select for the Add Movement Input node, then connect the Axis Value pin of the InputAxis MoveRight node to the Scale Value pin of the Add Movement Input node.
- Drag off from the Make Rotator node's Rotator Return Value pin and in the drop down menu search for and select the Get Right Vector node, then drag off from the Vector Return Value pin and plug it into the Add Movement Input node's World Direction pin.
- Your completed Movement Input Events Blueprint graph will look as it does below.
- Right-click on the Event Graph, then search for and select for the InputAxis LookUp event node, then right-click, search for and select for the Add Controller Pitch Input node.
- Drag off from the Axis Value pin of the Input Axis LookUp node and connect to the Val pin of the Add Controller Pitch Input node. Drag off from the execution output pin of the InputAxis LookUp node and connect to the execution input pin of the Add Controller Pitch Input node.
- Right-click on the Event Graph, then search for and select for the Input Axis Turn event node, then right-click, search for, and select the Add Controller Yaw Input node.
- Drag off from the Axis Value pin of the Input Axis Turn node and connect to the Val pin of the Add Controller Yaw Input node. Drag off from the execution output pin of the Input Axis Turn node and connect to the execution input pin of the Add Controller Yaw Input node.
-
Click Compile and Save.
Work-In-Progress Blueprint
At this point in the process, your Blueprint graph should resemble the image shown below.
You will need to set up some custom logic that will move your character when an Input key such as W,A,S, or D is pressed.
-
Navigate to Edit > Project Settings > Input, then from the Bindings Category click the + sign next to Action Mappings to add the following Action mappings.
Action Mapping Key Value Jump Space Bar Crouch Left Ctrl Sprint Left Shift -
In the Bindings Category click the + sign next to Axis Mappings to add the following Axis Mappings.
Axis Mapping Key Value Scale MoveForward W 1.0 S -1.0 MoveRight D 1.0 A -1.0 Turn Mouse X 1.0 LookUp Mouse Y -1.0 -
In your PlayerCharacter.h, declare the following class methods
protected: //Called for forwards/backward input void MoveForward(float InputAxis); //called for left/right side input void MoveRight(float InputAxis);
-
In your PlayerCharacter.Cpp, implement the following logic for your APlayerCharacter::MoveForward, and APlayerCharacter::MoveRight class methods.
void APlayerCharacter::MoveForward(float AxisValue) { if ((Controller != nullptr) && (AxisValue != 0.0f)) { // Find out which way is forward const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); // Get forward vector const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); AddMovementInput(Direction, AxisValue); } } void APlayerCharacter::MoveRight(float AxisValue) { if ((Controller != nullptr) && (AxisValue != 0.0f)) { // Find out which way is right const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); // Get right vector const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); // Add movement in that direction AddMovementInput(Direction, AxisValue); } }
-
Next, navigate to the APlayerCharacter::SetupPlayerInputComponent method, and implement the following code.
//Called to bind functionality to input void APlayerCharacter::SetupPlayerInputComponent(UInputComponent*) { Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight); PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput); PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput); }
-
Compile your code
Work-In-Progress Code
PlayerCharacter.h
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"
UCLASS()
class CHARACTERMOVEMENT_API APlayerCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Spring Arm Component to follow the camera behind the player
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USpringArmComponent* SpringArmComp;
//Player follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UCameraComponent* CameraComp;
//Called for forwards/backward input
void MoveForward(float InputAxis);
//called for left/right side input
void MoveRight(float InputAxis);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
PlayerCharacter.Cpp
#include "PlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
APlayerCharacter::APlayerCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Instantiating your class Components
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
//Set the location and rotation of the Character Mesh Transform
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Attaching your class Components to the default character's Skeletal Mesh Component.
SpringArmComp->SetupAttachment(GetMesh());
CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName);
//Setting class variables of the spring arm
SpringArmComp->bUsePawnControlRotation = true;
//Setting class variables of the Character movement component
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
}
// Called when the game starts or when spawned
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
void APlayerCharacter::MoveForward(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// Get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, AxisValue);
}
}
void APlayerCharacter::MoveRight(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// Get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// Add movement in that direction
AddMovementInput(Direction, AxisValue);
}
}
// Called every frame
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward",this, &APlayerCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
Creating Input Key Mappings and Input Action Events
Now that you have completed the Blueprint scripting logic for your Move Forward, Move Right, Look Up, and Turn input events, you will need to finish implementing your Action Mappings for your Jump, Crouch, and Sprint input events.
- Right click on the Event Graph, then in the All Actions for this Blueprint drop down menu, search for and select your Jump Action Event.
-
Drag off from your InputAction Jump node's Pressed execution pin, then in the drop down menu, search for and select the Character function Jump.
-
Connect the execution pin of your InputAction Jump node to the execution pin of your Jump node.
- Drag off from the InputAction Jump node's Released execution pin, and in the drop down menu, search for and select the Character function Stop Jumping.
-
Right click on the Event Graph and in the drop down menu, search for and select the Input Action Sprint.
-
Navigate to the Components tab, then click and drag your Character Movement Component on to the Event Graph.
- Drag off the Character Movement pin and from the drop down menu, search for and select Set Max Walk Speed. Set the Max Walk Speed float value to 1000, then connect the Pressed Execution pin of the InputAction Sprint node to the Input Execution pin of the Set Max Walk Speed.
- Drag off from the Character Movement pin, then search for and select for another set Max Walk Speed node. Set the Max Walk Speed float value to 600, and then connect the Released execution pin of the InputAction Sprint node to the Set Max Walk Speed execution input pin.
-
Right-click on the Event Graph, and in the drop down menu search for and select your Input Action Crouch.
-
Drag off from the InputAction Crouch node's Pressed execution pin, then in the drop down menu, search for and select the Character class function Crouch.
- Drag off from the InputAction Crouch node's Released execution pin, then in the drop down action menu search for and select the Character class function Un Crouch.
-
Compile and Save your Blueprint.
Completed Blueprint
Your completed Blueprint should resemble the image shown below.
Now that you have completed the Code scripting logic for your Move Forward, Move Right, Look Up, and Turn input events, you will need to finish implementing your Action Mappings for your Jump, Crouch, and Sprint input events.
-
Navigate to your PlayerCharacter.h file and declare the following class methods
protected: //Sets Character Movement Speed to Sprint values. void BeginSprint(); //Sets Character Movement Speed back to default speed values. void EndSprint(); //Request for Character to Crouch void BeginCrouch(); //Request for Character to EndCrouch void EndCrouch();
-
In your PlayerCharacter.cpp file, implement the following logic for your APlayerCharacter::BeginSprint and APlayerCharacter::EndSprint class methods.
void APlayerCharacter::BeginSprint() { GetCharacterMovementComponent()->MaxWalkSpeed = 1000.0f; } void APlayerCharacter::EndSprint() { GetCharacterMovementComponent()->MaxWalkSpeed = 600.f; }
-
Implement the following logic for your APlayerCharacter::BeginCrouch and APlayerCharacter::EndCrouch methods.
void APlayerCharacter::BeginCrouch() { Crouch(); } void APlayerCharacter::EndCrouch() { UnCrouch(); }
-
Navigate to your APlayerCharacter:: ::SetupPlayerInputComponent method and declare the following code:
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { PlayerInputComponent->BindAction("Jump",IE_Pressed,this,&ACharacter::Jump); PlayerInputComponent->BindAction("Jump",IE_Released,this,&ACharacter::StopJumping); PlayerInputComponent->BindAction("Crouch",IE_Pressed,this,&APlayerCharacter::BeginCrouch); PlayerInputComponent->BindAction("Crouch",IE_Released,this,&APlayerCharacter::EndCrouch); PlayerInputComponent->BindAction("Sprint",IE_Pressed,this,&APlayerCharacter::BeginSprint); PlayerInputComponent->BindAction("Sprint",IE_Released,this,&APlayerCharacter::EndSprint); }
-
Compile your code.
Finished Code
PlayerCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"
UCLASS()
class CHARACTERMOVEMENT_API APlayerCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Spring Arm Component to follow the camera behind the player
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class USpringArmComponent* SpringArmComp;
//Player follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UCameraComponent* CameraComp;
//Called for forwards/backward input
void MoveForward(float InputAxis);
//Called for left/right side input
void MoveRight(float InputAxis);
//Sets Character Movement Speed to Sprint values.
void BeginSprint();
//Sets Character Movement Speed back to default speed values.
void EndSprint();
//Request for Character to Crouch
void BeginCrouch();
//Request for Character to EndCrouch
void EndCrouch();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
PlayerCharacter.cpp
#include "PlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
APlayerCharacter::APlayerCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Instantiating your class Components
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
//Set the location and rotation of the Character Mesh Transform
GetMesh()->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, -90.0f), FQuat(FRotator(0.0f, -90.0f, 0.0f)));
// Attaching your class Components to the default character's Skeletal Mesh Component.
SpringArmComp->SetupAttachment(GetMesh());
CameraComp->SetupAttachment(SpringArmComp,USpringArmComponent::SocketName);
//Setting class variables of the spring arm
SpringArmComp->bUsePawnControlRotation = true;
//Setting class variables of the Character movement component
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCharacterMovement()->bIgnoreBaseRotation = true;
}
// Called when the game starts or when spawned
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
void APlayerCharacter::MoveForward(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// Get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, AxisValue);
}
}
void APlayerCharacter::MoveRight(float AxisValue)
{
if ((Controller != nullptr) && (AxisValue != 0.0f))
{
// Find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// Get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// Add movement in that direction
AddMovementInput(Direction, AxisValue);
}
}
void APlayerCharacter::BeginSprint()
{
GetCharacterMovementComponent()->MaxWalkSpeed = 1000.0f;
}
void APlayerCharacter::EndSprint()
{
GetCharacterMovementComponent()->MaxWalkSpeed = 600.f;
}
void APlayerCharacter::BeginCrouch()
{
Crouch();
}
void APlayerCharacter::EndCrouch()
{
UnCrouch();
}
// Called every frame
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward",this, &APlayerCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAction("Jump",IE_Pressed,this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump",IE_Released,this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Crouch",IE_Pressed,this,&APlayerCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch",IE_Released,this,&APlayerCharacter::EndCrouch);
PlayerInputComponent->BindAction("Sprint",IE_Pressed,this, &APlayerCharacter::BeginSprint);
PlayerInputComponent->BindAction("Sprint",IE_Released,this, &APlayerCharacter::EndSprint);
}
Setting up the Game Mode
The GameMode defines the game's set of rules. These rules include what default pawn the player will spawn as when the game is launched. You will need to set up these rules in order to spawn the Player Character that you have created.
- In the Content Browser, click the Add/Import button and create a new Blueprint class, when the Pick a Parent Class menu appears, select Game Mode Base and name your new Gamemode class Bp_GameMode.
-
Double click Bp_GameMode to open it's class defaults, then navigate to the Details panel Classes category, and for the Default Pawn Class select Bp_PlayerCharacter.
-
Compile and Save your Blueprint.
-
Navigate to Edit > Project Settings > Maps & Modes, and in the Default Modes category, select Bp_GameMode.
The Game Mode defines the game's set of rules. These rules include what default pawn the player will spawn as when the game is launched. You will need to set up these rules in order to spawn the Player Character that you have created.
- In the Editor navigate to your C++ classes folder. Right click your CharacterMovementGameModeBase and from the C++ Class Actions menu, select Create Blueprint class based on your CharacterMovementGameModeBase, then name your new Gamemode class Bp_GameMode.
-
Double click Bp_GameMode to open it's class defaults, then navigate to the Details panel Classes category, and for the Default Pawn Class select Bp_PlayerCharacter.
-
Compile and Save your Blueprint.
-
Navigate to Edit > Project Settings > Maps & Modes, and in the Default Modes category, select Bp_GameMode.
Creating the Locomotion Blend Space
Now that you have a character that possesses the input functionality to move in your level, you will need to create Blend Spaces for your movement states (sprinting, jogging, and crouch walking).
Blend Spaces are special assets that allow for blending of animations based on the values of two inputs. You will create your own blend spaces that move between forward and backward and left and right movements based on the character's movement speed or direction.
-
In the Content Browser, click Add/Import > Animation > BlendSpace, when prompted to pick a skeleton, choose the UE4_Mannequin_Skeleton, then name your Blend Space Locomotion_BS.
-
Double click to open the Locomotion_BS Blend Space. In the Asset Details tab, navigate to the Axis Settings category, then select the arrows adjacent to the Horizontal Axis and the Vertical Axis to see more variable details.
-
In the Horizontal Axis settings, change the Name variable to Direction, then set the Minimum Axis Value to -180, and the Maximum Axis Value to 180.
-
In the Vertical Axis Settings, change the Name variable to Speed, then set the Maximum Axis Value to 1000.
-
Compile and Save.
-
Navigate to the Asset Browser, then within the filters search bar type Idle_Rifle_Hip, then drag and drop the Idle_Rifle_Hip asset into the Blend Space at Direction 0.0, and Speed 0.0.
- Repeat the previous step, inserting the Idle_Rifle_Hip asset into:
- Direction 180, Speed 0.
- Direction 90, Speed 0.
- Direction -90, Speed 0.
- Direction -180, Speed 0.
- In the Asset Browser tab Filters search bar type Walk_Fwd_Rifle_Ironsights, then drag and drop the Walk_Fwd_Rifle_Ironsights asset into the Blend Space at Direction 0.0, and Speed 250.
- Next, in the Asset Browser tab Filters search bar type Walk_Lt_Rifle_Ironsights, then drag and drop the Walk_Lt_Rifle_Ironsights asset into the Blend Space at Direction -90 and Speed 250.
- In the Asset Browser tab Filters search bar type Walk_Rt_Rifle_Ironsights, then drag and drop the Walk_Rt_Rifle_Ironsights asset into the Blend Space at Direction 90 and Speed 250.
- In the Asset Browser tab Filters search bar type Walk_Bwd_Rifle_Ironsights, then drag and drop theWalk_Bwd_Rifle_Ironsights asset into the Blend Space at Direction 180 and Speed 250, then drag and drop it again at Direction -180 and Speed 250.
- In the Asset Browser tab Filters search bar type Jog_Fwd_Rifle, then drag and drop the Jog_Fwd_Rifle asset into the Blend Space at Direction 0.0, and Speed 500.
- Navigate to the Asset Browser, then within the filters search bar type Jog_Lt_Rifle, then drag and drop the Jog_Lt_Rifle asset into the Blend Space at Direction -90, and Speed 500.
- Navigate to the Asset Browser, then within the filters search bar type Jog_Rt_Rifle, then drag and drop the Jog_Rt_Rifle asset into the Blend Space at Direction 90, and Speed 500.
- From the asset browser search bar filter type Jog_Bwd_Rifle_Ironsights, then drag and drop an Jog_Bwd_Rifle_Ironsights asset into the Blend Space at Direction 180 and Speed 500, then drag an additional Jog_Bwd_Rifle_Ironsights at Direction -180 and Speed 500.
- Complete your Blend Space by navigating to the asset browser, search for and select Sprint_Fwd_Rifle_Ironsights, then drag and drop an Sprint_Fwd_Rifle_Ironsights asset into the Blend Space at Direction 0 and Speed 1000,
-
Compile and Save.
Your completed Locomotion_BS will look as shown in the image below!
Creating the Crouch Locomotion Blend Space
With your Locomotion Blend Space complete, you will now need to create your Crouch Locomotion Blend Space.
-
In the Content Browser, click Add/Import > Animation > BlendSpace, when prompted to pick a skeleton, choose the UE4_Mannequin_Skeleton, then name your Blend Space LocomotionCrouch_BS.
-
Double click to open the Locomotion_BS Blend Space. In the Asset Details tab, navigate to the Axis Settings category, then select the arrows adjacent to the Horizontal Axis and the Vertical Axis to see more variable details.
-
In the Horizontal Axis settings, change the Name variable to Direction, then set the Minimum Axis Value to -180, and the Maximum Axis Value to 180.
-
In the Vertical Axis Settings, change the Name variable to Speed, then set the Maximum Axis Value to 300.
-
Repeat the previous steps from the Creating The Locomotion Blend Space section, and place your assets into the Blend Space at the values below.
Animation Asset Name Speed Direction Crouch_Idle_Rifle_Hip 0 0, 90, -90, 180, -180 Crouch_Walk_Fwd_Rifle_Hip 300 0 Crouch_Walk_Lt_Rifle_Hip 300 -90 Crouch_Walk_Rt_Rifle_Hip 300 90 Crouch_Walk_Bwd_Rifle_Hip 300 180, -180 -
Save and Compile your LocomotionCrouch_BS
Your completed LocomotionCrouch_BS Blend Space will look as shown in the image below.
Creating the Animation Blueprint
You will require an Animation Blueprint that will determine what character animations to play based on the current actions by the player. Additionally, you will need to set up a State Machine to create a Walk and Crouch state then set up the transitions between each of them.
-
In the Content Browser, click Add/Import > Animation > Animation Blueprint, and when prompted to pick a skeleton, choose the UE4_Mannequin_Skeleton, then name your Animation Blueprint PlayerCharacter_AB.
-
Double-click PlayerCharacter_AB to open the Animation Blueprint. In the My Blueprint tab, navigate to the Variables category and select the adjacent + sign to create two boolean variables named Is Crouched and Is Jumping.
-
Click the Event Graph tab, then drag off from the Return Value pin of the Try Get Pawn Owner node, then from the drop down menu search for and select Cast to BP_PlayerCharacter.
-
Drag off from the As BP_PlayerCharacter return pin of the Cast to BP_PlayerCharacter node, then search for and select Get Is Crouched.
-
Navigate to the My Blueprint tab, then from the variables category drag and drop the Is Crouched variable on to the return pin of the Get Is Crouched node.
- Drag off from the Cast To BP_PlayerCharacter's Output execution pin, and connect it to the Input execution pin of the Set Is Crouched node.
-
Drag off from the As BP_PlayerCharacter return pin of the Cast to BP_PlayerCharacter node, then from the All Actions drop down menu search for and select Get Pressed Jump.
-
From the MyBlueprint tab, navigate to the variables category, then drag and drop the Is Jumping variable on to the return pin of the Get Pressed Jump node.
- Drag off from the Set Is Crouched node Output execution pin, and connect it to the Input execution pin of the Set Is Jumping node.
-
Compile and Save your PlayerCharacter_AB Animation Blueprint.
-
From the Variables category select the adjacent + sign to create two float variables named Speed and Direction.
-
Drag off from the As BP_PlayerCharacter return pin of the Cast to BP_PlayerCharacter node, then click to enable the context sensitive box, and search for and select Get Velocity.
-
Drag off from the Get Velocity node's Return Value pin, and from the Actions drop down menu, search for and select Vector Length.
- In the My Blueprint tab, drag and drop the Speed variable on to the vector Return Value pin of the Vector Length node.
- Next, connect the Execution output pin of the Set Is Jumping node to the execution input pin of the Set Speed node.
-
Drag off from the As BP_PlayerCharacter return pin of the Cast to BP_PlayerCharacter node, then search for and select Get Actor Rotation.
-
Drag off from the GetActorRotation node's rotator return value pin and from the drop down Actions menu, search for and select Calculate Direction.
- Next, drag off from the vector Return Value pin of the Get Velocity node, and plug it into the Velocity input pin from the Calculate Direction node.
- From the My Blueprint tab, click and drag the Direction variable and drop it onto the float Return Value pin of the Calculate Direction node.
- Next, connect the Execution output pin from the Set Speed node to the execution input pin of the Set Direction node.
- Drag off from the Event Blueprint Update Animation node's execution output pin and connect it to the Cast To BP_PlayerCharacter node.
-
Compile and Save your PlayerCharacter_AB Animation Blueprint.
Your completed Animation Event Graph will resemble the image below
Creating the Animation State Machines
State Machines provide a graphical visualization of organizing the animation of a Skeletal Mesh into a series of States. These states are governed by Transition Rules that control how to blend from one state to another. You will use your boolean variables to transition between the different locomotion Blend Spaces that you created in the earlier sections.
- Inside your PlayerCharacter_AB Animation Blueprint, navigate to the Anim Graph.
The AnimGraph is used to evaluate a final pose for the skeletal mesh.
-
Right-click on the AnimGraph and from the Actions drop down menu, search for and select Add New State Machine.
-
Rename your State Machine node to Locomotion, then double-click the node to open its graph. Drag off from the Entry pin, and from the Actions drop down menu, select Add State.
- Name the new state Movement, then double-click to open the Movement State Node. Navigate to the Asset browser, and search for Locomotion_BS, then click and drag it into the graph.
- From the My Blueprint tab, click and drag your Speed float variable into the Blend Space's Speed vertical axis.
- Navigate back to the My Blueprint tab, then click and drag your Direction float variable into the Blend Space's Direction horizontal axis.
- Connect the Animation Pose output pin to the Animation Pose Result pin of the Output Animation Pose node.
-
Compile and Save.
-
Navigate back to the Locomotion state machine, then click and drag the Movement state node to create a new Animation state named Crouch Movement.
- Double click the Transition rule node to open its Anim Graph.
- From the My Blueprint Tab, navigate to the variables category and drag your Is Crouched boolean onto the Can Enter Transition input pin from the Result node.
- Navigate back to the Locomotion state machine graph, and double click on the Crouch Movement state to open its anim graph. From the Asset browser, search for your LocomotionCrouch_BS, then click and drag it into the graph.
- From the My Blueprint Tab, navigate to the variables category and drag your Speed and Direction float variables into their respective pins on the LocomotionCrouch_BS, then connect your LocomotionCrouch_BS Animation Pose into your Output Animation Pose Result pin.
- Compile and Save your Animation Blueprint.
- Navigate back to the Locomotion AnimGraph then click and drag off the CrouchMovement Animation state and connect it to the Movement Animation State.
- Double click the Transition rule node to open it's Anim Graph.
- From the My Blueprints tab, click and drag the Is Crouched boolean variable onto the Anim Graph, select Get Is Crouched, then drag off from its output pin and in the drop down actions menu search for and select NOT Boolean.
- Next, connect the NOT Boolean's return pin to the Can Enter Transition input pin.
-
Compile and Save.
-
Navigate back to the Anim Graph, then click and drag off from the Movement Animation state and select Add State.
- Rename this Animation State to Jump, then double-click to open its animation graph. Navigate to the Asset Browser and search for Jump_From_Jog, then drag it into the graph.
- Finish your Jump Animation State by connecting your Play Jump_From_Jog Animation Pose to your Output Animation Pose Result pin.
- Navigate back to the Locomotion Anim Graph, then double-click the Movement to Jump Transition rule node to open its Anim Graph.
- Inside the Transition Rule Anim Graph, navigate to the My Blueprints tab, and drag your Is Jumping Boolean variable onto the Result node's Can Enter Transition input pin.
- Navigate back to the Locomotion Anim Graph, then click and drag off the Jump anim node and create a transition to the Movement Anim Graph node.
- Double-click the Jump to Movement Transition rule node to open it's Anim Graph.
- From the My Blueprints tab, click and drag the Is Crouched, and Is Jumping Boolean variable onto the Anim Graph, then drag off each of their output pins and from the drop down actions menu search for and select NOT Boolean.
-
Drag off the NOT Boolean output pin and search for and select for the AND Boolean.
-
Connect both the Is Crouched, and Is Jumping NOT Boolean node output pins into the AND Boolean node's input pins, then connect the AND boolean's output pin into the Result node's Can Enter Transition Input pin.
- Navigate back to the Locomotion Anim Graph, then click and drag off the Crouch node and create a transition to the Jump node.
- Double-click the Crouch Movement to Jump Transition rule node to open it's Anim Graph.
- Inside the CrouchMovement to Jump transition rule Anim Graph, navigate to the My Blueprint tab, then click and drag your Is Jumping bool variable onto the Result node's Can Enter Transition input pin.
- Navigate back to the Locomotion Anim Graph, then drag off from the Jump node to create a transition to the CrouchMovement node.
- Double-click the Jump to Crouch Movement Transition rule node to open it's Anim Graph.
- From the My Blueprints tab, click and drag the Is Crouched and Is Jumping Boolean variables onto the Anim Graph.
- Drag off from the Is Jumping Boolean output pin and from the actions drop down menu search for and select the NOT Boolean, then drag off from the Is Crouched Boolean output pin and search for and select for the AND Boolean. Connect the Is Jumping NOT Boolean output pin into the AND Boolean input pin, then connect the AND Boolean output pin into the Can Enter Transition Result pin to complete the transition rule.
- Your completed Locomotion AnimGraph will resemble the image below.
- Navigate to the AnimGraph and connect the Locomotion Pose Output pin into the Output Pose Result pin.
-
Compile and Save.
-
From the Content Browser, double-click your Bp_PlayerCharacter to open it's class defaults, then in the Components tab select the Mesh component, then navigate to the Details panel Animation category and in the Anim Class variable drop down menu, search for and select PlayerCharacter_AB.
Result
Now is the time to test your logic. From the toolbar select Play (PIE), you will be able to control your character's movement using the W,A,S,D keys. To sprint, click and hold your left shift key, to crouch, hold left ctrl, and to jump press the spacebar.