This site uses strictly necessary cookies. More Information

X# Rotating transform more than 180 degrees

Hi, I was wondering why my transform won't keep rotating after 180 degrees. The rotation must be set and not something like Transform.Rotate due to the circumstances in my game.

Here is the code: using UnityEngine; using System.Collections;

```
public class Test : MonoBehaviour {
public float speed = 10;
public float time = 0.25f;
void Start () {
StartCoroutine("Wait");
}
void Rotate () {
Vector3 rot = transform.rotation.eulerAngles;
rot += new Vector3(speed, 0, 0);
transform.rotation = Quaternion.Euler(rot);
StartCoroutine("Wait");
}
IEnumerator Wait (){
yield return new WaitForSeconds (time);
Rotate ();
}
}
```

Here is the result: https://gyazo.com/bd66665ac525b74afab5879c1a5d1a1a

You can rotate objects using quaternions ins$$anonymous$$d of euler angles. To rotate an object you just need to multiply it's rotation (which is a quaternion) by a quaternion representing rotation.

Something like that:

```
transform.rotation = transform.rotation * Quaternion.AngleAxis(5f, Vector3.up);
```

**Answer** by unity_bTkJGG31azdikQ
·
Jan 23, 2020 at 06:51 PM

I know this is four years old, but for posterity.

Converting from euler angle to quaternion and back won't always get you what you started with, since there's more than one way to represent a rotation with euler angles (well, with quaternions too). When I tested this code, it actually wouldn't rotate more than one step past 90 degrees. After setting `rot`

to `(100, 0, 0)`

, storing it as a quaternion and pulling it back into an euler representation, it turns out to be `(80, 180, 180)`

, and when you add 10 degrees to the x component of that, you move backward. Unity stores the quaternion representation and just gives the euler representation when it's asked for. In general, euler angles should only be used for humans to input and read rotations/orientations.

I don't understand why Transform.Rotate won't work for you, but if you must avoid it, you might try storing the euler representation as a class variable to ensure it stays as expected. The downside here is that if you're rotating around multiple axes, you might also get unexpected results because euler rotations mean rotations around specific axes in a particular order. If you're just doing one axis for a given object, you should be fine, though.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Why does assigning a quaternion to transform rotation work but calling transform.rotation. doesnt? 1 Answer

Euler Angles in code / inspector different (both local & global) 1 Answer

What is the difference between setting transform.eulerAngles and transform.rotation? 1 Answer

Rotate 2D ignoring certain angle 0 Answers