Character Movement Direction Issue Between 180 and -135 Degrees

Hello all, first time posting here :slight_smile:

I have been trying to find a solution to this issue for a while. I do understand what the root cause is, but I am failing to find a robust solution.

I have a character that is using root motion (not necessarily relevant to the issue as far as I know?). The movement works fine for most cases. For calculating the direction of movement, I am using the following which gives me the direction that player wants to move towards, based on their input:

const FVector InputDirection = CharacterRef->GetActorForwardVector() * InputForwardValue + CharacterRef->GetActorRightVector() * InputRightValue;

InputForwardValue and InputRightValue may be 1.0, 0, or -1.0.

Then, I am calculating the angle of movement (between -180.0 and 180.0) to pass into my blendspace, using the following line in my AnimInstance class:

float Angle = CalculateDirection(ActiveMovementDirection, CharacterRef->GetActorRotation());

The problem: When I am having the character move back (InputForwardValue is -1.0), Angle gets calculated as 180.0. Makes sense. When I also press A to set InputRightValue to -1.0 as well, the calculated angle will become -135.0. Makes sense. The issue is, the interpolation will happen from 180.0 to -135.0 which means, my character starts rotating counter-clockwise 315 degrees.

I know that to fix this, I need to have it consider 180.0 โ€œlikeโ€ -180.0, so that it can interpolate it from -180.0 to -135.0 in this case. Is there any support in the engine to handle this? What am I missing? It seems like this should be a simple thing to do.

I was not able to find out any interpolation function in the Unreal Engine API that I could directly use. So instead I ended up implementing my own that resolves the issue for my project.

Basically, I wrote a method similar to the other interpolation functions you can find in FMath that works with angles (float values in inclusive range [-180.0, 180.0]). In this function, I find out which quadrant the source and target angles are located in, and make a decision based on that. If, letโ€™s say, the source angle is in quadrant 4 (135) and target angle is in quadrant 3 (-135), interpolation should go clockwise from 135 to 180, which then becomes -180, and then interpolation continues from -180 to -135.