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.
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.
Warrior
, deriving from Character
:UCLASS() class CH6_API AWarrior : public ACharacter { GENERATED_BODY() };
BP_Warrior
, based on your Warrior
class.GameMode
class as follows:GameMode
class, and name of your choice (say, BP_GameMode
).BP_GameMode
Blueprint class that you have created to edit it.BP_GameMode
blueprint, and select your Blueprinted BP_Warrior
class as default Pawn
Class.SetupPlayerInputComponent
function for the AWarrior
class as follows:void AWarrior::SetupPlayerInputComponent(UInputComponent* Input) { check(Input); Input->BindAxis( "Forward", this, &AWarrior::Forward ); }
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 ); } }
AWarrior::Back
, AWarrior::Left
, and AWarrior::Right
.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.
3.17.167.114