180 Degree Rotation and Mathf.Lerp

When the unit rotates exactly 180 degrees Mathf.Lerp seems to provide the wrong results. I noticed this in both the MoveAction and ShootAction.

In the case of the MoveAction, the unit will walk backward for a moment and then quickly rotate to face the target direction.

In the case of the ShootAction, at least in my implementation, the unit doesn’t move for the first half of the shoot state timer. At the halfway point the unit immediately snaps to the target direction for the second half of the shoot state timer.

After some research, I found that Mathf.Slerp may be more appropriate for smoothing rotation. I changed my implementations to use Mathf.Slerp and this appears to have corrected the issue with rotating precisely 180 degrees.

I just wanted to call out this quirk in case there is good reason to use Mathf.Slerp for interpolation of rotation in general as opposed to Mathf.Lerp.

I found these two diagrams that make sense to me. However, as a beginner, I’m not sure if this accurately represents the difference between these two functions in the context of rotation interpolation.

Mathf.Lerp:
124592-rotationdirect

Mathf.Slerp:
124593-rotationangle

We should be able to reproduce this issue by placing a unit on a grid position such as (0, 1), facing (0, 2), then attempting to move the unit to (0, 0).

9 Likes

Yup that’s exactly right!
That’s the negative side effect of using Lerp, which works with a regular Vector, and the solution is indeed to use Slerp, which works with a direction Vector so it doesn’t have the issue of the Vector smoothly interpolating to 0,0 before suddenly flipping to the other side.

8 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms