Logo

Ljung

.dev

A project using this tutorial series is tracked on GitHub. The revision after this part is d8b66fb.

Hej!

If you're using Unreal Engine and you have a character that you want to move then you've probably stumbled upon the built-in Character and CharacterMovement classes. Together these classes allow you to easily set up a game character complete with movement, gravity, physics, animations, and networking.

However this can also be very daunting as you might find yourself struggling to find the right combination of properties and values to get just the right feel for your game. Looking through the source code is extremely helpful, however between the two classes there are almost 19k lines of code, and CharacterMovement.cpp alone takes up 13k (!) of those. While lots of it is chalked up to comments and formatting, the breadth is the meticulous engineering efforts of 50+ contributors.

But the pair of classes are not a one-size-fits-all. Notably they're probably too heavy for something like crowd controlling hundreds of units in an RTS, and integrating custom movement options (e.g. wall-climbing, vaulting etc.) might not seem straight-forward.

For me, I wanted to understand how exactly basic movement works and so I set out to build a basic first-person shooter game (similar in vein to old-school Quake and Doom games) featuring basic but common movement operations.

The list of movement operations I'm going to implement are:

  • Basic movement + camera look

  • Simple collision and slide along surfaces

  • Gravity

  • Jumping

  • Step down (walk down slopes/stairs)

  • Step up (walk up slopes/stairs)

  • Limit climbable angles (steepness)

  • Moving platforms

  • Friction (e.g. ice, mud)

  • Swimming

Note: This series does not go too much into depth on some of the Unreal and mathematical details, so it is best if you're already familiar with the engine in general, as well as basic vector math and physics.

Setup

To start off this tutorial series we will create a fresh project and setup all the boilerplate needed to get going with the movement code.

I will be using Unreal Engine 5.0.3 on Windows 10 however the instructions should be mostly the same for newer/other versions.

Boilerplate classes

To start implementing movement code we start with a blank C++ project. The basic classes we need are:

  • A game mode class (AGameMode, here named ACactusGameMode)

  • A player controller class (APlayerController, here named ACactusPlayerController)

  • A pawn class (APawn, here named ACactusPlayerPawn)

  • A pawn movement component (UPawnMovementComponent, here named USimpleMovementComponent)

In addition I've also created the following boilerplate classes that are for now unrelated to movement:

  • ACactusGameState derived from AGameStateBase

  • ACactusPlayerState derived from APlayerState

Here are the required source files:

  • CactusGameMode.h

cpp
1#pragma once
2
3#include "CoreMinimal.h"
4#include "GameFramework/GameModeBase.h"
5#include "CactusGameMode.generated.h"
6
7UCLASS()
8class CACTUS_API ACactusGameMode final : public AGameModeBase
9{
10  GENERATED_BODY()
11};
12
  • CactusGameMode.cpp

cpp
1#include "CactusGameMode.h"
2
  • CactusPlayerController.h

cpp
1#pragma once
2
3#include "CoreMinimal.h"
4#include "CactusPlayerController.generated.h"
5
6UCLASS()
7class CACTUS_API ACactusPlayerController final : public APlayerController
8{
9  GENERATED_BODY()
10};
11
  • CactusPlayerController.cpp

cpp
1#include "CactusPlayerController.h"
2
  • CactusPlayerPawn.h

cpp
1#pragma once
2
3#include "CoreMinimal.h"
4#include "GameFramework/Pawn.h"
5#include "CactusPlayerPawn.generated.h"
6
7UCLASS()
8class CACTUS_API ACactusPlayerPawn final : public APawn
9{
10  GENERATED_BODY()
11
12public:
13  UPROPERTY(EditDefaultsOnly)
14  class UCapsuleComponent* CapsuleComponent;
15
16  UPROPERTY(EditDefaultsOnly)
17  class UCameraComponent* CameraComponent;
18
19  UPROPERTY(EditDefaultsOnly)
20  class USimpleMovementComponent* MovementComponent;
21
22  ACactusPlayerPawn();
23};
24
  • CactusPlayerPawn.cpp

cpp
1#include "CactusPlayerPawn.h"
2
3#include "SimpleMovementComponent.h"
4#include "Camera/CameraComponent.h"
5#include "Components/CapsuleComponent.h"
6
7ACactusPlayerPawn::ACactusPlayerPawn()
8{
9  CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Collider"));
10  SetRootComponent(CapsuleComponent);
11
12  CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
13  CameraComponent->SetupAttachment(CapsuleComponent);
14
15  MovementComponent = CreateDefaultSubobject<USimpleMovementComponent>(TEXT("Movement"));
16}
17
  • SimpleMovementComponent.h

cpp
1#pragma once
2
3#include "CoreMinimal.h"
4#include "GameFramework/PawnMovementComponent.h"
5#include "SimpleMovementComponent.generated.h"
6
7UCLASS()
8class CACTUS_API USimpleMovementComponent final : public UPawnMovementComponent
9{
10  GENERATED_BODY()
11};
12
  • SimpleMovementComponent.cpp

cpp
1#include "SimpleMovementComponent.h"
2

Blueprint classes

Next up is creating blueprint classes based on these C++ classes. This is technically not required but helps for rapid iteration when testing properties and debugging.

Make sure to do this for every C++ class (except for the movement component which is C++ only).

Where you put it doesn't matter too much but I've separated the game mode and game state into a Core folder, and controller, pawn, and player state into a Player folder.

After you've created the BP classes go into your game mode BP (here named BP_CactusGameMode) and set the default classes to your newly created BP classes:

Then go into Edit -> Project Settings -> Maps & Modes and set "Default GameMode" to your GameMode BP class.

Additionally to get more realistic character proportions I've edited the player pawn BP with the following properties:

  • Capsule component "Capsule Half Height" set to 88

  • Capsule component "Capsule Radius" set to 30

  • Camera location Z set to 60

It should look similar to this:

Finally create a test level, place a player start actor and start the game and you should be looking through the camera of your player pawn BP and be unable to move or look (if you can fly around then you're still using the default game mode base and controller).

Enhanced Input plugin

For input this series of tutorials will utilize the experimental Enhanced Input plugin instead of the traditional built-in mapping system. Enabling it is a simple process. First go to Edit -> Plugins and enable the plugin:

Then restart the editor, go to Edit -> Project Settings -> Input, and set "Default Player Input Class" and "Default Input Component Class" to the Enhanced Input variants:

Now everything should be up and connected and we're ready to implement input and movement, which we will do in the next part.