Rotation Direction

Hi All,

I’ve been looking around for a while and can’t find any definitive answer for how to rotate in a certain direction. So I’ve done a bit of testing and exploring different ways to make this work.

In this example, the currentRotation is 0, so the rotation goes clockwise to -90. Which is great.

public Vector3 currentRotation;

   while (true)
        {
            currentRotation.y = Mathf.Lerp(currentRotation.y, -90, Time.deltaTime);
            transform.eulerAngles = currentRotation;
            yield return null;
        }

Though when I do this afterwards, once the currentRotation is now -90 and I want to go back to 0 it takes the long way round, so goes all the way down to -359 then onto 0.

public Vector3 currentRotation;

   while (true)
        {
            currentRotation.y = Mathf.Lerp(currentRotation.y, 0, Time.deltaTime);
            transform.eulerAngles = currentRotation;
            yield return null;
        }

Now my solution is to try and use rotateAround with a Vector3 direction such as:

    transform.RotateAround(transform.position, Vector3.down,  Time.deltaTime);    

This allows me to choose the direction. The issue here now is I can’t get the rotation to stop, considering I want to go to -90 how do I make this stop? As doing something like this doesn’t work, as it’s a negative value:

while (transform.localEulerAngles.y > -90)
            {
            transform.RotateAround(transform.position, Vector3.down,  Time.deltaTime);           
            yield return null;
        }      

I hope this all makes sense and someone can possibly help with a solution somewhere! Going to try different rotation methods to see if the direction works differently. Some people say Lerp takes the quickest route always, but this can’t be true otherwise my rotation wouldn’t take the long way round to go from -90 to 0.

I’ve deleted my earlier answer because it was incorrect. I’ve had success with Quaternion.Lerp in the past, but I admit I’m no expert in quaternions. Try Quaternion.RotateTowards.

public float turningRate = 30f; //Max turn rate in degrees per second

private Quaternion targetRotation = Quaternion.Euler(transform.eulerAngles.x, -90f, transform.eulerAngles.z);

    while (true)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turningRate * Time.deltaTime);
        }

Thanks to DMG’s answer on an earlier post. He goes more in depth on how , and more importantly why, you should use quaternions instead of vector3s when dealing with rotation.

I’ve tried to fit the code to your specific use case, but it may be wiser to just use DMG’s code here:

// Maximum turn rate in degrees per second.
public float turningRate = 30f; 
// Rotation we should blend towards.
private Quaternion _targetRotation = Quaternion.identity;
// Call this when you want to turn the object smoothly.
public void SetBlendedEulerAngles(Vector3 angles)
{
  _targetRotation = Quaternion.Euler(angles);
}

private void Update()
{
   // Turn towards our target rotation.
   transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turningRate * Time.deltaTime);
}

The following worked for my purposes. All the code does is tell me if my gameObject is rotating clockwise or counterclockwise if Vector3.up is pointing toward the camera.

        angle = Vector3.SignedAngle(transform.forward, preVector, Vector3.up);
        preVector = transform.forward;

        if (angle * -1 > 0)
            rotationDirection = 1;
        if (angle * -1 < 0)
            rotationDirection = -1;