Axis Mappings – keyboard, mouse and gamepad directional input for an FPS character

There are two types of input mapping: Axis mappings and Action mappings. Axis mappings are inputs that you hold down for an extended period of time to get their effect (for example, holding the W key to move the player forward), while Action mappings are one-off inputs (such as pressing the A key on the gamepad to make the player jump). In this recipe, we'll cover how to set up keyboard, mouse, and gamepad axis-mapped input controls to move an FPS character.

Getting ready

You must have a UE4 project, which has a main character player in it, and a ground plane to walk on, ready for this recipe.

How to do it...

  1. Create a C++ class, Warrior, deriving from Character:
    UCLASS()
    class CH6_API AWarrior : public ACharacter
    {
      GENERATED_BODY()
    };
  2. Launch UE4, and derive a Blueprint, BP_Warrior, based on your Warrior class.
  3. Create and select a new Blueprint for your GameMode class as follows:
    1. Go to Settings | Project Settings | Maps & Modes.
    2. Click on the + icon beside the default GameMode drop-down menu, which will create a new Blueprint of the GameMode class, and name of your choice (say, BP_GameMode).
    3. Double-click the new BP_GameMode Blueprint class that you have created to edit it.
  4. Open your BP_GameMode blueprint, and select your Blueprinted BP_Warrior class as default Pawn Class.
  5. To set up the keyboard's input driving the player, open Settings | Project Settings | Input. In the following steps, we will complete the process that drives the player forward in the game:
    1. Click on the + icon beside the Axis Mappings heading.

      Tip

      Axis Mappings supports continuous (button-held) input, while Action Mappings supports one-off events.

    2. Give a name to the Axis mapping. This first example will show how to move the player forward, so name it something like Forward.
    3. Underneath Forward, select a keyboard key to assign to this Axis mapping, such as W.
    4. Click on the + icon beside Forward, and select a game controller input to map to moving the player Forward (such as gamepad Left Thumbstick Up).
    5. Complete Axis Mappings for Back, Left, and Right with keyboard, gamepad, and, optionally, mouse input bindings for each.
  6. From your C++ code, override the SetupPlayerInputComponent function for the AWarrior class as follows:
    void AWarrior::SetupPlayerInputComponent(UInputComponent* Input)
    {
      check(Input);
      Input->BindAxis( "Forward", this, &AWarrior::Forward );
    }
  7. Provide a Forward function inside your AWarrior class as follows:
    void AWarrior::Forward( float amount )
    {
      if( Controller && amount )
      {
        // Moves the player forward by an amount in forward direction
        AddMovementInput(GetActorForwardVector(), amount );
      }
    }
  8. Write and complete functions for the rest of the input directions, AWarrior::Back, AWarrior::Left, and AWarrior::Right.

How it works…

The UE4 Engine allows wire-up input events directly to C++ function calls. The function called by an input event are member functions of some class. In the preceding example, we routed both the pressing of the W key and holding of the gamepad's Left Thumbstick Up to the AWarrior::Forward C++ function. The instance to call AWarrior::Forward on is the instance that routed the controller's input. That is controlled by the object set as the player's avatar in the GameMode class.

See also

  • Instead of entering the Forward input axis binding in the UE4 editor, you can actually code it in from C++. We'll describe this in detail in a later recipe, Adding Axis and Action Mappings from C++.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114