Spinning out of control!

So I am having an odd issue with my character’s rotation. I am trying to make him rotate his y axis based on the angle I have the left joystick on my controller set to. That part was working fine. To add a bit of polish though, I decided to add a lerp to make the transition from one angle to the next not instant. The lerp worked fine for all directions except left. Basically anything from 181 to 359 degrees makes the character spin super fast over and over. From 0 to 180 it works fine though.

Here is the line of code controlling all this:

playerGraphic.transform.localEulerAngles = new Vector3 
(0,Mathf.Lerp(playerGraphic.transform.localEulerAngles.y, 
(Mathf.Atan2(Input.GetAxis("Horizontal"), 
Input.GetAxis("Vertical"))* Mathf.Rad2Deg), .2f),0);

To break it down, basically the Mathf.Atan2 gets the angle of the joystick and turns it in to a number between 0 and 360 degrees. The Lerp is just to smoothly transition from the previous angle to the new angle.

It works in 3 directions, but anything to the left half of the joystick makes him spin out super fast.

Any ideas?

Use Mathf.LerpAngle instead of Mathf.Lerp when lerping angles

The problem with using Mathf.Lerp for angles is that it returns a number between from and to and does not take into account that angles are cyclic. For instance Mathf.Lerp(0, 359, r) will return values between 0 and 359 instead of return values 359 through 360 (0).