Download

Would it be better to use the PlayerController or the Character for movement?

I’m trying to make a top-down shooter game and I want my character to rotate relative to where the mouse cursor is. I’m having trouble doing this inside the Character file, so I’m wondering if it is easier or more efficient to use the PlayerController file for movement.

To my knowledge this depends:
Do you want to switch to another player pawn with a different control scheme without switching the PC? If you do, do pawn-specific inputs in the pawn.
If you don’t care about that, you could do inputs in the PC.

You could also do it any way you want.
This is how I achieved what you want in an older project:



FRotator AYourCharacter::CalculateRotation()
{
    if (APlayerController* PC = Cast<APlayerController>(GetController()))
    {
        FVector2D MousePosition;
        PC->GetMousePosition(MousePosition.X, MousePosition.Y);

        FVector2D CharacterOnScreen;
        PC->ProjectWorldLocationToScreen(GetActorLocation(), CharacterOnScreen);

        FVector2D MouseResult;
        MouseResult.X = -(MousePosition.Y - CharacterOnScreen.Y);
        MouseResult.Y = MousePosition.X - CharacterOnScreen.X;

        float Angle = FMath::RadiansToDegrees(FMath::Acos(MouseResult.X / MouseResult.Size()));

        if (MouseResult.Y < 0)
        {
            Angle = 360 - Angle;
        }

        FRotator Rotation(0, Angle, 0);

        return Rotation;
    }
    return FRotator(0, 0, 0);
}


And in the Tick Function of the Character:



SetActorRotation(CalculateRotation());