Trying to Match Character Facing to Input Direction in Top Down Game

I’m trying to make a top-down character who will rotate and aim at the direction of the right-stick input.

I’m having two problems:

  1. I’m trying to compare character facing and input, and turn the character to the lesser-of-the-two-angles towards the input. When I fire it up, it works at first, but then frequently gets stuck at cardinal points (0 and 180). The facing starts flipping between 0/1 or 179/180 every frame and so the character gets locked into that position. I’ve attached the script I’m using to control turning. Can someone see what I’m doing wrong (or give me a better way to do what I’m trying to do)?

  2. I’m having problems with the character facing “skipping” past the input facing if I set the rotation speed too high. I assume this is related to frame-rate and the character jumping over several degrees per frame and missing the “match” with the facing. I converted facing and input to integers to get them to match at all, but if I go above 45-60 rotation speed (presumably whatever my frame-rate is), the character will frequently continue rotating, and do so until it happens to get a “match.” Is there a good way to prevent this from happening? And is there a way to make aiming more precise than 360 integers?

Thanks!

Researching this more, it seems like the second problem would be solved by rotational sweep, which isn’t supported (or easy). Although I think when it’s purely a 2D rotation, would be relatively easy compared to along a 3D rotation. Is there a way to write a decent 2D sweep?

  1. instead of rounding to compare floats, use IsNearlyEqual to compare floats.

  2. use RInterpTo. that one node will do everything you are trying to do.

Thank you so much. I ended up using RInterp to Constant to avoid acceleration and deceleration and plugged in my Rotation Speed variable, but this is exactly what I wanted (and took five minutes instead of the hours I spent doing all that other mess…). Thanks again!

From Top Down project exemple :

Configure character movement

GetCharacterMovement()->bOrientRotationToMovement = true; // Rotate character to moving direction
GetCharacterMovement()->RotationRate = FRotator(0.f, 640.f, 0.f);
GetCharacterMovement()->bConstrainToPlane = true;
GetCharacterMovement()->bSnapToPlaneAtStart = true;

I would add one thing to that: make sure your pawn does not “Use Controller Rotation [x]” or it will block the “Orient to movement” property.