Enhanced input, move forward: Third Person template project.

Hi!

I’m using Unreal 5.2.1 and I’m trying to understand Enhanced input with C++. I have create a test project which inherits from Third Persona Template.

Reading the code, I don’t understand this:


void AInputActionTestCharacter::Move(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D MovementVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	
		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}

I don’t know why, if it uses EAxis::X with ForwardDirection, then if use Y axis with ForwardDirection in AddMovementInput(ForwardDirection, MovementVector.Y);.

By the way, I have copied this code into my own project and, using the keys W and S, when I press W the character moves to the right, and with S to the left.

Is there something wrong in the code?
Could someone explain this to me?

Note: I have found the modifier Swizzle Input Axis Values, but I don’t understand the word Swizzle.

Thanks!

1 Like

I don’t know if the code has errors or not, but there is something you need to know about it. So you’re using a 2D Axis Input Action (Vector2D), here’s how it works:

  • X = right/left (A/D)
  • Y = forward/backward (W/S)

The confusion comes from 2D input vs. 3D world directions.

In input, pressing W gives you (0, 1), meaning “go forward” on the Y axis.
But in the 3D world, forward movement is along the character’s X axis (GetForwardVector).

So we have to “Swizzle” or convert the input Y into movement along the X direction.

That’s why we only apply Swizzle for W/S. (forward vector)
For A/D (left/right), the input 2daxis X already matches the 3D right/left direction, so no swizzle is needed for A/D. (right vector).