- Create a C++ class and select Character as the parent class. Then, hit Next:
- Under the Name property, type in Warrior, and then click on Create Class:
We will dive into doing the implementation after we do some setup inside UE4.
- Launch UE4 and right-click on the Warrior class. Then, select Create Blueprint class based on Warrior:
- From the menu that pops up, set the name to BP_Warrior and then select Create Blueprint Class:
- Close the Blueprints menu that was just opened.
- Create and select a new Blueprint for your GameMode class, by going to Settings | Project Settings | Maps & Modes:
- Click on the + icon beside the default GameMode drop-down menu, which will create a new Blueprint of the GameMode class. Put a name of your choice (say, BP_GameMode):
- Double-click the new BP_GameMode Blueprint class that you created to edit it. It can be found in the Contents\Blueprints folder from the Content Browser.
- Once your BP_GameMode blueprint is opened, select your Blueprinted BP_Warrior class as the Default Pawn Class:
The location of the Default Pawn Class property.
- To set up the keyboard's input, which will drive the player, open Settings | Project Settings | Input. (Input can be found under the Engine subsection.) In the following steps, we will complete the process that drives the player forward in the game.
- Click on the + icon beside the Axis Mappings heading:
Axis Mappings supports continuous (button-held) input, while Action Mappings supports one-off events.
- Give a name to the Axis mapping. This first example will show how to move the player forward, so name it something like Forward.
- Underneath Forward, select a keyboard key to assign to this Axis mapping, such as W.
- Click on the + icon beside Forward and select a game controller input to map so that you can move the player forward (such as Gamepad Left Thumbstick Up):
- Complete Axis Mappings for Back, Left, and Right with keyboard, gamepad, and, optionally, mouse input bindings for each:
- Now, return to the .h file. We will need to add some new function definitions, which we will be writing:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Warrior.generated.h"
UCLASS()
class CHAPTER_06_API AWarrior : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AWarrior();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent*
PlayerInputComponent) override;
// Movement functions
void Forward(float amount);
void Back(float amount);
void Right(float amount);
void Left(float amount);
};
- From your C++ code, override the SetupPlayerInputComponent function for the AWarrior class, as follows:
#include "Components/InputComponent.h"
// ...
// Called to bind functionality to input
void AWarrior::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAxis("Forward", this,
&AWarrior::Forward);
PlayerInputComponent->BindAxis("Back", this, &AWarrior::Back);
PlayerInputComponent->BindAxis("Right", this, &AWarrior::Right);
PlayerInputComponent->BindAxis("Left", this, &AWarrior::Left);
}
- Provide a Forward function inside your AWarrior class, as follows:
void AWarrior::Forward(float amount)
{
// Moves the player forward by an amount in forward
// direction
AddMovementInput(GetActorForwardVector(), amount);
}
- Write and complete functions for the rest of the input directions, that is, AWarrior::Back, AWarrior::Left, and AWarrior::Right:
void AWarrior::Back(float amount)
{
AddMovementInput(-GetActorForwardVector(), amount);
}
void AWarrior::Right(float amount)
{
AddMovementInput(GetActorRightVector(), amount);
}
void AWarrior::Left(float amount)
{
AddMovementInput(-GetActorRightVector(), amount);
}
- Return to Unreal and compile your code. Afterwards, play and game and confirm that you can now move with both the keyboard and the left thumbstick on your gamepad: