Vector2 Lerp not working when angle is 180 degrees

So I am trying to code a top down controller where the character rotates towards where my joystick is pointing.

The problem I am having is that it doesn’t work when the angle between the old vector and new one is 180 degrees. I understand why it doesn’t work, I just can’t think of another way to do it that wouldn’t have this problem. Was thinking of just handling the edge case and doing something different when the angle was 180 but that just doesn’t feel right.

Here is the code I have:

xVel = Input.GetAxis ("Horizontal");
yVel = Input.GetAxis ("Vertical");

Vector2 newUp = new Vector2(xVel, yVel).normalized;
        
transform.up = Vector2.Lerp(transform.up, newUp, plStats.rotationFactor * Time.deltaTime);

Thank you for any help you can give me!

So, if my understanding of the problem is correct, the issue here is that you’re directly lerping the direction vector, which - when lerping between opposite vectors, like (0,-1) and (0,1) - makes the character stand still, until the lerped value reaches (0,0) and the character simply rotates by 180 degrees immediately.

124592-rotationdirect.png

This works good for small rotations, but the bigger the change in your vector the worse the result will look - near the beginning and the end of lerping the object will barely rotate at all. The closer to the middle, the faster the rotation will be. If you want smooth rotation, you need something else.

Instead of working directly on a directional vector, try calculating and lerping between rotation angles. Get the rotation angle between your current transform.upand some reference vector (like (0,1)), get the angle between the newUp and that same reference vector, and then lerp between these two angles. You can get the lerped directional vector by multiplying the reference vector by a rotation quaternion: Quaternion.Euler(0.0f, 0.0f, angle) * Vector2.up.

124593-rotationangle.png

Use Vector2.Angle or Vector2.SignedAngle to get the angle of rotation between vectors.