Disable diagonal movement 2d top down game

Well, could do it like this on your pawn:


// On pawn tick, otherwise you would tick on two separate events anyway (MoveRight and MoveUp).
void AYourPawn::Tick(float InDeltaSeconds) {
	Super::Tick(InDeltaSeconds);
	
	// Get the axis value of an input axis binding. Note that it must be bound on this input component.
	const float RightVal = InputComponent->GetAxisValue(TEXT("MoveRight"));
	const float UpVal = InputComponent->GetAxisValue(TEXT("MoveUp"));

	// Check if movement input is up or right based on input size.
	const bool  bGoRight = FMath::Abs(RightVal) > FMath::Abs(UpVal);

	// Either use the actor rotation, camera rotation or if nothing ever rotates just the absolute world vector.
	const FVector MovementDirection = bGoRight ? UKismetMathLibrary::GetRightVector(GetActorRotation()) : UKismetMathLibrary::GetUpVector(GetActorRotation());

	// Get the speed
	const float MovementSpeed = bGoRight ? RightVal : UpVal;

	// Add movement input to be processed by UE default implementation
	AddMovementInput(MovementDirection, MovementSpeed, false);
}

In blueprints this is just the same if you search for the method names you get a node. input axis bindings can be made in the project settings → input.

1 Like