Instant rotation bug when rotating clockwise
Hi there,
I am trying to use AddRelativeTorque to give smooth Z rotation to a game object:
- Rotate counter clockwise when given left horizontal input (pressing the "A" key or left on a left controller stick) 
 
- Rotate clockwise when given right horizontal input (the "W" key or pressing right on a left controller stick) 
 
- When given no input, rotate the game object back to default (in this case Z rotation = 0) 
I'm also trying to limit the rotation, so that the force isn't added anymore when it hits a defined maximum rotation.
I have it working when rotating counter clockwise from 0 degrees towards 360 degrees. But weird things start happening, occasionally, when trying to rotate clockwise from 360 towards 0 degrees. Sometimes "transform.localEulerAngles.z" becomes negative for a frame or two and then my rotation jumps from 360 to my desired rotation (instant rotation):

Here is my code for rotating clockwise:
 if (Input.GetAxis("Horizontal") > 0) // Turn right (rotate clockwise)
 {
        if (transform.localEulerAngles.z > 360 - maxRotationZDegrees)
        {
            rb.AddRelativeTorque(new Vector3(0.0f, 0.0f, -1 * horizontalAxis * strafingRotationSpeed), ForceMode.Force);
        }
        else
        {
            // Set rotation to maximum and cancel accumulating force
            transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, 360 - maxRotationZDegrees);
            rb.angularVelocity = Vector3.zero;
        }
 }
I've been working on this problem and skimming unity forums for days and I still can't quite figure this out. I keep reading that local euler angles can't be negative but always stay within 0-360 degrees. Can anyone enlighten me why counter clockwise rotation (0-360 degrees) doesn't cause any glitches, but why clockwise rotation (360-0 degrees) can sometimes cause "transform.localEulerAngles.z" to be negative? 
How can I get my z axis to rotate smoothly clockwise and not instantly jump from 0 degrees to the end goal rotation (maxRotationZDegrees)? Thank you.
Even if someone could refer me to a similar bug / solution page I would be grateful. I haven't been able to find anyone else having a bug like this, but it it roadblocking my project currently. Any help would be much appreciated.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                