Language:
Page Info
Skill Level:
Engine Version:

2.5 - Implementing Character Jumping

Choose your OS:

In general, Action Mappings deal with inputs for discrete events, they allow you to map inputs to a "friendly name" that can later be bound to event-driven behavior. The end effect is that pressing and/or releasing a key, mouse button, or keypad button directly triggers game behavior.

During this step, we're going to add the ability for our character to jump by setting up the input action mapping for the space bar.

Jump Action Mapping

  1. In the Edit menu, click on Project Settings.

  2. Under the Engine heading on the left side of the Project Settings tab, click on Input.

  3. Under Bindings, click on the plus sign next to Action Mappings.

  4. Click on the arrow to the left of Action Mappings.

  5. Type "Jump" into the text field that appears, then click on the arrow to the left of the text box to expand the action binding options.

  6. In the dropdown menu, select Space Bar from the Keyboard dropdown list.

  7. Your input settings should now look like the following:

    JumpActionMapping_SpaceBar.png

  8. Close the Project Settings menu.

Implement Input Handling

If you look inside the interface file (*.h) for the Character base class, you'll notice that there is built-in support for character jumping. Character jumping is tied to the bPressedJump variable, so all we need to do is set that boolean to true when the jump action is pressed, and false when the jump action is released. You'll need to add the following two functions to accomplish this:

  • StartJump

  • StopJump

Go back to Visual Studio to add code to your FPSCharacter class.

  1. In FPSCharacter.h, add the following public function declarations:

    // Sets jump flag when key is pressed.
    UFUNCTION()
    void StartJump();
    
    // Clears jump flag when key is released.
    UFUNCTION()
    void StopJump();
  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);
    
        // Sets jump flag when key is pressed.
        UFUNCTION()
        void StartJump();
    
        // Clears jump flag when key is released.
        UFUNCTION()
        void StopJump();
    };
  3. In FPSCharacter.cpp, add the following function definitions:

    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }
    
    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }
  4. Now, add the following code to SetupPlayerInputComponent to bind your Jump action to the newly written functions:

    // Set up "action" bindings.
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
    PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
  5. 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);
    
        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    
        // Set up "action" bindings.
        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
        PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
    }
    
    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);
    }
    
    void AFPSCharacter::StartJump()
    {
        bPressedJump = true;
    }
    
    void AFPSCharacter::StopJump()
    {
        bPressedJump = false;
    }

Testing Character Jumping

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. You should be able to jump throughout the map.

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