Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท C++ Gameplay

Input and Character Movement in C++

Move a character with Enhanced Input in Unreal Engine C++. Input Actions, mapping contexts, AddMovementInput, jump, camera look and the maths of acceleration.

A character that does not respond to the player is just a statue. In Unreal Engine 5 the recommended input system is Enhanced Input, which replaced the older Action and Axis mappings. Instead of asking "is W pressed?", you define Input Actions (Move, Look, Jump), group key bindings into Input Mapping Contexts, and bind C++ functions to those actions.

This lesson walks through a third-person character in C++. The C++ is read-only because the engine is not available on this site; the Python stand-ins let you experiment with the movement maths.

The pieces

  • Input Action (IA): an asset describing one thing the player can do, with a value type (Digital bool, Axis1D float, Axis2D Vector2D or Axis3D).
  • Input Mapping Context (IMC): an asset mapping keys to Input Actions, with optional modifiers (Negate, Swizzle Input Axis Values, Dead Zone) and triggers (Pressed, Held, Released).
  • Enhanced Input component: replaces the plain UInputComponent. Your project must have the Enhanced Input plugin enabled, and the module EnhancedInput in Build.cs.

For WASD you create IA_Move (Axis2D). In the context, map W with a Swizzle Input Axis Values modifier (YXZ) so it feeds the Y axis, S with Swizzle plus Negate, D to X, and A to X with Negate. The gamepad left stick maps directly with no modifiers.

Registering the context

C++
void AMyCharacter::BeginPlay()
{
    Super::BeginPlay();

    if (APlayerController* PC = Cast<APlayerController>(GetController()))
    {
        if (auto* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
        {
            Subsystem->AddMappingContext(DefaultContext, 0);
        }
    }
}

DefaultContext is a UPROPERTY(EditAnywhere) TObjectPtr<UInputMappingContext> you fill in the Blueprint subclass. The number is the priority: higher priority contexts win when two map the same key.

Binding actions

C++
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    if (auto* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent))
    {
        Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
        Input->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
        Input->BindAction(JumpAction, ETriggerEvent::Started,   this, &ACharacter::Jump);
        Input->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
    }
}

ETriggerEvent::Triggered fires every frame while the input is active; Started fires once when it begins; Completed when it ends.

Move and Look

C++
void AMyCharacter::Move(const FInputActionValue& Value)
{
    const FVector2D Input = Value.Get<FVector2D>();
    const FRotator YawOnly(0.f, GetControlRotation().Yaw, 0.f);

    const FVector Forward = FRotationMatrix(YawOnly).GetUnitAxis(EAxis::X);
    const FVector Right   = FRotationMatrix(YawOnly).GetUnitAxis(EAxis::Y);

    AddMovementInput(Forward, Input.Y);
    AddMovementInput(Right,   Input.X);
}

void AMyCharacter::Look(const FInputActionValue& Value)
{
    const FVector2D Look = Value.Get<FVector2D>();
    AddControllerYawInput(Look.X);
    AddControllerPitchInput(Look.Y);
}

Notice that Move does not change the location itself. AddMovementInput only queues a direction; the UCharacterMovementComponent consumes it during its own tick, applying acceleration, friction, gravity, floor detection and collision. That is why a character's walk speed lives in the movement component (MaxWalkSpeed, MaxAcceleration, BrakingDecelerationWalking, JumpZVelocity, AirControl) rather than in your code.

Using only the yaw of the control rotation keeps the character walking along the ground when the camera looks up or down.

Moving without the Character class

For a plain APawn there is no built-in movement logic. You can add a UFloatingPawnMovement component, or move yourself in Tick:

C++
AddActorWorldOffset(Direction * Speed * DeltaTime, /*bSweep=*/true);

bSweep = true stops the actor against blocking collision instead of teleporting through walls.

The maths of acceleration

The movement component does not jump straight to full speed. Each frame it moves velocity toward the target speed by MaxAcceleration * DeltaTime. The stand-in below models that, with braking when input stops.

Python Stand-in: acceleration and braking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Unreal uses centimeters and centimeters per second, so 600 cm/s is 6 m/s, about a brisk jog.

Normalizing diagonal input

Pressing W and D together gives input (1, 1) with length 1.41. If you added that directly you would move 41 percent faster diagonally. AddMovementInput clamps the combined scale to 1, but if you write your own movement, normalize.

Python Stand-in: diagonal speed fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Forgetting the mapping context. Without AddMappingContext nothing fires, and there is no error.
  • Missing EnhancedInput in PublicDependencyModuleNames. The code will not link.
  • Moving by editing location in Tick for a Character. It fights the movement component; use AddMovementInput.
  • Frame-dependent look speed. Mouse input is already per-frame deltas; do not multiply it by DeltaTime, but do multiply gamepad stick look by it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an Input Mapping Context contain?

    A mapping context maps keys and buttons to Input Actions and can transform values with modifiers.
  2. 2. What does AddMovementInput do on a Character?

    The movement component applies acceleration, friction, gravity and collision using the queued input.
  3. 3. Which ETriggerEvent fires once when a jump button is first pressed?

    Started fires on the frame the input begins, Triggered every frame while active, Completed when it ends.
  4. 4. Why use only the controller's yaw to compute forward in Move?

    Using yaw only keeps movement on the horizontal plane even when the camera pitches.

Summary

  • Enhanced Input uses Input Actions, Input Mapping Contexts and an Enhanced Input component.
  • Add the context in BeginPlay through the local player subsystem, bind actions in SetupPlayerInputComponent.
  • AddMovementInput feeds the UCharacterMovementComponent; tune speed there.
  • Normalize your own diagonal movement and use DeltaTime for time-based motion.

Next: Memory and Garbage Collection