This site uses strictly necessary cookies. More Information

X- Home /

# Why Y and Z eulerAngles are changed from 0 to 180(and vice versa) when object rotates by X axis

Good day everyone,

I have a spaceship object which is able to be rotated by 360 of any Axis. Moreover, Camera smoothly follows ship rotation. Well, when I rotate spaceship by X axis, every 180 degree of X, Z and Y eulerAngles are changed from 0 to 180 suddenly, so Camera rapidly rotates as well by Y and Z axis, that is not meant to be . How can I avoid it? Here is the code:

```
// Calculate the current rotation angles
wantedRotationAngleY = target.eulerAngles.y;
currentRotationAngleY = transform.eulerAngles.y;
wantedRotationAngleX = target.eulerAngles.x;
currentRotationAngleX = transform.eulerAngles.x;
wantedRotationAngleZ = target.eulerAngles.z;
currentRotationAngleZ = transform.eulerAngles.z;
// Damp the rotation around the y-axis
currentRotationAngleX = Mathf.LerpAngle(currentRotationAngleX, wantedRotationAngleX, rotationDamping * Time.deltaTime*10);
currentRotationAngleY = Mathf.LerpAngle(currentRotationAngleY, wantedRotationAngleY, rotationDamping * Time.deltaTime*10);
currentRotationAngleZ = Mathf.LerpAngle(currentRotationAngleZ, wantedRotationAngleZ, rotationDamping * Time.deltaTime*10);
//Camera rotation and position
// Convert the angle into a rotation
Quaternion currentRotation = Quaternion.Euler (currentRotationAngleX, currentRotationAngleY, currentRotationAngleZ);
//Quaternion currentRotation = Quaternion.Lerp(transform.rotation, target.rotation, Time.deltaTime * 2.0f);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward;
transform.rotation = currentRotation;
```

Thanks in advance

**Answer** by tanoshimi
·
Dec 17, 2014 at 07:58 PM

I suspect it's because you're trying to convert Quaternions to/from Euler angles. Can you not achieve what you're trying to as follows?:

```
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, rotationDamping * Time.deltaTime*10);
transform.position = target.position;
transform.position -= transform.forward;
```

That is simple and right solvation. Thanks. But anyway, I want to understand why Z and Y are changed?

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

Flip over an object (smooth transition) 3 Answers

using a custom rotation in "Instantiate" 4 Answers

Rotation by 90 degrees not exactly 90 1 Answer

How do I make a child object move from the parent's rotation? 2 Answers

Orbit Collision Axis 0 Answers