Language:
Page Info
Skill Level:
Engine Version:

2.3 - Implementing Character Movement Functions

Choose your OS:

In this step, we're going to set-up our Player Input Component and implement the following functions in the FPSCharacter class:

  • MoveForward

  • MoveRight

Movement Function Interfaces

Now that you've set-up Axis mappings for your FPSCharacter, you can switch to your project in Visual Studio.

  1. In FPSCharacter.h, add the following function declarations under SetupPlayerInputComponent.

    // Handles input for moving forward and backward.
    UFUNCTION()
    void MoveForward(float Value);
    
    // Handles input for moving right and left.
    UFUNCTION()
    void MoveRight(float Value);

    The UFUNCTION macro (located above each of these functions) makes the engine aware of these functions so that they can be included in serialization and other engine functionality.

  2. FPSCharacter.h should now look like the following:

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "GameFramework/Character.h"
    #include "FPSCharacter.generated.h"
    
    UCLASS()
    class FPSPROJECT_API AFPSCharacter : public ACharacter
    {
        GENERATED_BODY()
    
    public:
        // Sets default values for this character's properties
        AFPSCharacter();
    
    protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;
    
    public:
        // Called every frame
        virtual void Tick( float DeltaSeconds ) override;
    
        // Called to bind functionality to input
        virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
    
        // Handles input for moving forward and backward.
        UFUNCTION()
        void MoveForward(float Value);
    
        // Handles input for moving right and left.
        UFUNCTION()
        void MoveRight(float Value);
    
    };

Movement Function Implementations

In a typical FPS control scheme, the character's movement axes are relative to the camera. "Forward" movement means "the direction that the camera is pointing" and "right" means "to the right of the direction that the camera is pointing." You are going to use the PlayerController to get the character's control rotation. Also, your MoveForward function will ignore the pitch component of the control rotation and restrict your input to the XY plane so as to guarantee that your character will move along the ground while you are looking up or down.

  1. In FPSCharacter.cpp, add the following lines to the SetupPlayerInputComponent function under Super::SetupPlayerInputComponent(PlayerInputComponent):

    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    }

    An InputComponent is a component that defines how to handle input data. An InputComponent can be attached to an actor that wants to receive input.

  2. Add the following MoveForward function definition in FPSCharacter.cpp.

    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
  3. Add the following MoveRight function definition in FPSCharacter.cpp.

    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }
  4. FPSCharacter.cpp should now look like the following:

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #include "FPSProject.h"
    #include "FPSCharacter.h"
    
    // Sets default values
    AFPSCharacter::AFPSCharacter()
    {
        // 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;
    
    }
    
    // Called when the game starts or when spawned
    void AFPSCharacter::BeginPlay()
    {
        Super::BeginPlay();
    
        if (GEngine)
        {
            // Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
            GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
        }
    }
    
    // Called every frame
    void AFPSCharacter::Tick( float DeltaTime )
    {
        Super::Tick( DeltaTime );
    
    }
    
    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    }
    
    void AFPSCharacter::MoveForward(float Value)
    {
        // Find out which way is "forward" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
        AddMovementInput(Direction, Value);
    }
    
    void AFPSCharacter::MoveRight(float Value)
    {
        // Find out which way is "right" and record that the player wants to move that way.
        FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
        AddMovementInput(Direction, Value);
    }

Testing Character Movement

It's now time to compile and test your newly implemented character movement functions.

  1. Save the FPSCharacter header (*.h) and CPP (*.cpp) files in Visual Studio.

  2. Locate FPSProject in the Solution Explorer.

  3. Right-click on FPSProject and select Build to compile your project.

    BuildProject.png

  4. After the build finishes, open your FPSProject in Unreal Editor.

  5. Click the Play button in the Level Editor Toolbar. By now, you should be able to move forward, backward, left, and right.

  6. Press the Escape key or click the Stop button in the Level Editor to exit Play in Editor (PIE) mode.