Rotate an object more than 360 degrees

I know it is a simple thing but I am unable to set the rotation of an object to more than 360 degrees by code

I am using Euler angles, but it doesn’t matter how I set the rotation to more than 360 degrees it always converts it to local rotation between 0, 360 degrees. I need to go from 360 to 450 instead of 360 to 90.

So, how I put 450 degrees by code? I can set the 450 in the editor but as soon as I use the code, it converts it to 360

My code

    private void Update()
    {
        if(rotateCam)
        {


            interpolateAdvance += Time.deltaTime * camRotationSpeed;

            float currentAngle = Mathf.Lerp(shopCam.transform.localEulerAngles.y, targetDegrees, interpolateAdvance);
            Debug.Log("Current angle " + currentAngle);
            //shopCam.transform.localEulerAngles = new Vector3(0, currentAngle, 0);
            shopCam.transform.eulerAngles = new Vector3(0, currentAngle, 0);
            //shopCam.transform.rotation = Quaternion.Euler(0, currentAngle, 0);
            //shopCam.transform.localRotation = Quaternion.Euler(0, currentAngle, 0);
            if (interpolateAdvance>= 1) rotateCam = false;
        }
    }



I set the target Degrees this way.

        switch ((ShopCategory)category)
        {
            case ShopCategory.CHESTS:
                targetDegrees = 90;
                break;
            case ShopCategory.PREMIUM:
                targetDegrees = 180;
                break;
            case ShopCategory.WEAPONS:
                targetDegrees = 270;
                break;
            case ShopCategory.HEROES:
                targetDegrees = 450;
                break;
            case ShopCategory.FORTRESS:
                targetDegrees = 540;
                break;
            case ShopCategory.PASSIVES:
                targetDegrees = 630;
                break;
            case ShopCategory.SEASON:
                targetDegrees = 810;
                break;
        }

The thing is, 450° and 90° are the same. Any rotation value is taken modulo 360° internally (not actually, since internally Quaternions are used), which makes sense since there are no rotations outside of 360°. If you want to rotate something for visual purposes and always want it to rotate, for example, clockwise, then you need to implement that yourself. For example, rotating from 180° to 450° should rotate clockwise, but since 450° = 90° it would rotate counter-clockwise.
I’m not entirely sure if that’s really your problem tho, since you said you need to go from 360° to 450°, instead of 360° to 90°. What’s the difference? If you are at 360°, then that’s the exactly same as being at 0° (let’s call the direction forward), and rotating 90° further would in both cases (450° and 90°) make the object look right. The end orientation is the same.
What is your usecase? What exactly are you trying to do where this is a problem?

Also you can only set 450° in the editor, because Unity made an exception there. Internally Quaternions are used for efficiency purposes, as well as to prevent problems such as gimbal lock. Euler angles are far inferior overall, the only advantage of them is that they are easier to understand for humans. If Unity used Quaternions for the editor however, then the Euler values would jump all over the place, since Quaternion → Euler has multiple representations. Thus, they made an exception and actually work with whatever value you type into the inspector and keep track of that. However, other than having problems like gimbal lock and being less efficient performance wise, it’s the exact same thing as i tried to describe above. So either way, 450° is still the same orientation as 90°.
So is this about the direction of rotation, visually?

Essentially, if you have your own way that you want to represent rotations, just implement your own number system (just store it in a Vector 3 or something) and maintain your own numbers in parallel with Unity’s rotation- i.e. whenever you to update the rotation, then update both Unity’s rotation and your own. Whenever you read the rotation, just return your own.

Thanks for the replies! Sorry if I didn’t explain myself correctly.

Yes, this is about the direction of the rotation visually. I have a camera that should look at 3 points, 90, 180, 270.

They can go both ways, (right and left) and this can happen 7 times so an easy way to approach this is to make a linear interpolation between the target angle and the current angle. So it will always rotate towards the desired direction

These are my desired angles

  case ShopCategory.CHESTS:
                targetDegrees = 90;
                break;
            case ShopCategory.PREMIUM:
                targetDegrees = 180;
                break;
            case ShopCategory.WEAPONS:
                targetDegrees = 270;
                break;
            case ShopCategory.HEROES:
                targetDegrees = 450;
                break;
            case ShopCategory.FORTRESS:
                targetDegrees = 540;
                break;
            case ShopCategory.PASSIVES:
                targetDegrees = 630;
                break;
            case ShopCategory.SEASON:
                targetDegrees = 810;
                break;

So If I make a linear interpolation between 450 and 270 it will turn left, however, If I do a linear interpolation between 90 and 270 it will turn right. So there is the problem, If the user clicks on the right button the camera should rotate to the right always, and same for the left. So the problem is that forcing the system to interpret it as 360 degrees is killing the direction.

How I can do this then?

The problem comes with the direction of the rotation, even if I implement my own number system, the linear interpolation of the angles will still go the same way even If I want to do it on reverse

No it wouldnt. If you wanted to go from, say, 300° to 400° and you keep track of that yourself and interpolate between those numbers, and then the values saved in Unity are not relevant for the rotation direction. If it takes 10 frames to do this rotation, then the values between 300° and 400° would be 310, 320, 330, … , 390, 400°. If you input these into Unity as a set rotation (since you interpolate with your own values you only need to set the rotation) you are rotating in the correct way.

Unity editor stores the values you typed in rotation field for you, but internally it is 0-360. If you want to do like this, do it like they did

class UnlimitedRotation : MonoBehaviour {
[SerializeField] private Vector3 _euler;
public Vector3 euler {
  get => _euler;
set {
      _euler = value;
      transform.rotation = Quaternion.Euler(value);
    }
  }
  private void OnValidate() {
    this.euler = _euler;
  }
}

Thanks all for your replies.

The problem was that I was interpolating the current camera angle with the next position instead of the angle at the beginning. I will let it here so anyone with the same problem solves the issue!

1 Like