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.