- 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
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Mouse Character Rotation problem :( 1 Answer
GameObject facing fowards 1 Answer
Raycasting from object rotation? 1 Answer