Download

move an object in the direction it's facing?

I have an object that rotates along with my mouse, the camera is attached to it. Now I want the movement in the direction I rotated how would I go about that with axis inputs?
I replicated basic controls in the documentation, with

InputComponent->BindAxis("MoveForward", this, &ACameraPawn::MoveXAxis);
InputComponent->BindAxis("MoveRight", this, &ACameraPawn::MoveYAxis);

if (!CurrentVelocity.IsZero())
{
    FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
    SetActorLocation(NewLocation);       
}

But obviously it only moves in the same direction regardless of rotation.

GetActorForwardVector() is one option.

Another is to convert the rotation into the vectors you need.
Its a good idea to constrain any vectors/rotations you use to limit movement in just the direction you want.
e.g. Make sure the forward vector is only forward and not forward and up.

I believe the C++ versions of the First Person template or the ShooterGameExample do this, but I could be wrong.

In any case:


FRotator YawRotation = FRotator(0.0f, Rotation.Yaw, 0.0f);
FVector Forward = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X)

Here is the code that i used:



        const FRotator MoveRotation(0.f, GetControlRotation().Yaw, 0.f);
        const FVector MoveDirection = FRotationMatrix(MoveRotation).GetScaledAxis(EAxis::X);
        AddMovementInput(MoveDirection, 1.f);

Thanks guys that worked