How can I do it to move the pawn backward?

Hi!

I’m using Unreal Engine 5.3.2.

I need to do this: when the player presses a key I have to move an actor 50 units. I don’t care if the player holds the key pressed, it only will move 50 units. The next time the player presses the key, the actor will move another 50 units.

This is the input action to move the actor:

And this is the mapping context:

And the C++ code:

void ARSPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	// Set up action bindings
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent))
	{
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ARSPlayerController::MoveActionMethod);
		EnhancedInputComponent->BindAction(TurnAction, ETriggerEvent::Triggered, this, &ARSPlayerController::TurnActionMethod);
	}
}

And the method to move the actor in the player controller:

void ARSPlayerController::MoveActionMethod(const FInputActionValue& Value)
{
	if (TObjectPtr<ARSRobotPawn> ControlledPawn = Cast<ARSRobotPawn>(GetPawn()))
	{
		ControlledPawn->Move(Value.Get<bool>());
	}
}

And in the pawn:

void ARSRobotPawn::Move(bool bValue)
{
	if (!bIsMoving)
	{
		DestinationLocation = GetActorLocation();
		if (bValue)
		{
			DestinationLocation.X = DestinationLocation.X + GridCellSize;
		}
		else
		{
			DestinationLocation.X = DestinationLocation.X - GridCellSize;
		}
		

		bIsMoving = true;
	}
}

My problem is that the actor doesn’t move backward, it always move forward because the bValue is always True.

How can I do it to move the pawn backward?

Thanks.

Instead of Value Type being a Digital (bool), set it to Axis1D(float) (1st pic). So your 2nd pic would then send 1 for Up and a negated 1 for Down.

The next time the player presses the key, the actor will move another 50 units.

Not sure if that’s the part of the ask, but that’d be:

IA's Started -> isTimelinePlaying -> False -> Play Timeline from Start

There’s probably 10 different ways of doing it, though - Lerp, Tick interpolate, Timers. If it’s supposed to be a precise 50uus increments, I’d avoid navmesh. A timeline would be the most consistent, easy to use and still quite flexible.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.