This site uses strictly necessary cookies. More Information

X- Home /

# Issue with Euler angles

I have a vehicle with wheels.I need to rotate the 2 wheels for specific angles. Let's say, the user inputs the angle as 45 degrees, the wheels must rotate 45 degrees and stop. I am using AddRelativetorque to push the wheel. I am stuck with the issue of euler angles as x axis only moves from 0-90-0-360-270-360-... kind of like oscillating between 90 and 270. Can someone help me with this please?.

Are you rotating your wheels to steer your vehicle or to turn your wheels to move forward?

I'm not sure that I understand.. are you trying to track the euler angles so that you know when to stop applying force? And the euler angles just aren't returning values that you expect?

Yes that's exactly what's happening.Let's say I want the wheels to rotate 180 degrees and stop. But I am not getting 180 degree as an angle while checking the euler angles in x. I am sorry I wasn't clear with the issue.

**Answer** by unity_ek98vnTRplGj8Q
·
Feb 19 at 04:04 PM

If you want to track angle change over time you can't just read the Euler Angles every frame. I will try to briefly explain why, but you can skip to the end if you just want the solution.

Rotations are stored as *Quaternions* in Unity. Quaternions are just a fancy mathematical form for rotations. You don't need to know how a Quaternion works, but what you DO need to know is that for every possible rotation there is exactly ONE corresponding Quaternion. The problem with Quaternions is that they are hard to make sense of for a layman. When you look at a rotation in Unity you want to be able to tell what the rotation is around each axis, so Unity gives you the ability to grab the Euler Angles out of a Quaternion. Whenever you ask for rotation.eulerAngles, Unity converts the Quaternion representation into an Euler representation. One of the problems with Euler Angles, though, is that for any given rotation there are INFINITE different Euler representations. Some of these are easy to see, for example (0, 0, 0) is obviously the same as (360, 0, 0), but there are also more complicated equivalent rotations as well. When Unity gives you the Euler Angles, it tries to give you the simplest Euler representation it can, however, and this means that if you are querying for Euler Angles every frame you cannot rely on the Euler Angles themselves always following each other. For a small change in rotation, Euler Angles can still vary wildly.

So what can you do, rather than tracking the euler angles every frame? Instead, just read the Euler Angles of the *change in rotation*. Track the rotation from the previous frame, then compare it to your current rotation to get the *delta rotation*, then you can pull the Euler Angles to get the overall change in rotation. (Note that this will only work if the change in rotation per frame is relatively small).

```
private Quaternion prevRotation;
private float totalRotationAngleX;
void Update(){
Quaternion deltaRotation = transform.rotation / prevRotation;
float xAngleThisFrame = deltaRotation.eulerAngles.x;
totalRotationAngleX += xAngleThisFrame;
prevRotation = transform.rotation;
}
```

In `Quaternion deltaRotation = transform.rotation/prevRotation`

, we are taking the difference between 2 quaternions right?

Yes, when you want to "add" quaternions you multiply them and when you wan to get the difference between quaternions you divide them.

I tried this method, but in some frames I am getting xAngleThisFrame as 359 which is not what I want. And just in small rotation, the value of totalRotationAngleX reaches 5k. I am not sure what is wrong here

**Answer** by jackmw94
·
Feb 19 at 08:30 AM

If you want to turn your wheels a set number of degrees, use quaternions rather than euler angles. These are used to avoid gimbal lock (where your euler axes align), awkwardness with degrees wrapping around (i.e. trying to move from 320 degrees to 10 degrees and taking the long route) which might be your problem here and also using the quaternion functions make the code easier to read.

I believe the function you'll need here is AngleAxis, this takes arguments for the number of degrees and the axis around which to rotate.

```
// rotate 45 degrees around x axis:
transform.rotation = Quaternion.AngleAxis(45, Vector3.right) * transform.rotation;
```

A quaternion is a description of a rotation so this code will rotate your transform 45 degrees around the global right axis wherever it starts from. Have a look at the other static functions in the quaternion class as they're good to have in your toolbox for problems like these.

### 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.