- Home /
Getting the actual object rotation on just 1 axis
I'm trying extract the rotation in 1 axis of an object so I can combine it with another rotation.
The plan is to pass the rotation angle to Quaternion.AngleAxis to create the rotation quaternion in just that axis.
The issue is that the values on eulerAngles don't go from 0-360, they go from 0-90->90-0->360-270->270-360 in a clockwise direction. The other axis get flipped by 180 to make the rotation work. But I need a value that goes from 0-360 which is what works for Quaternion.AngleAxis.
This small code showcases the issue I'm having
float angleX = transform.eulerAngles.x;
Quaternion XRot = Quaternion.AngleAxis(angleX, Vector3.right);
transform.rotation = XRot;
This code should just rotate the object in the x axis the same way it was already set, so shouldn't do much, but because of the way the angles are returned this is not the case. When the object goes past 90 degrees the rotation locks up and doesn't go any further.
I've looked for a solution to this issue for a while now and I can't seem to find any information on how to do this properly.
Take a look at this answer for a plane HUD. $$anonymous$$ch is calculating the 'x' rotation.
http://answers.unity3d.com/questions/696271/how-create-modern-aircrafts-hud.html
Answer by ratamorph · Jul 26, 2014 at 07:55 PM
I fixed my issue, found a method to get the pitch, yaw, roll angles and created the following functions:
public static Vector3 GetPitchYawRollRad(Quaternion rotation)
{
float roll = Mathf.Atan2(2*rotation.y*rotation.w - 2*rotation.x*rotation.z, 1 - 2*rotation.y*rotation.y - 2*rotation.z*rotation.z);
float pitch = Mathf.Atan2(2*rotation.x*rotation.w - 2*rotation.y*rotation.z, 1 - 2*rotation.x*rotation.x - 2*rotation.z*rotation.z);
float yaw = Mathf.Asin(2*rotation.x*rotation.y + 2*rotation.z*rotation.w);
return new Vector3(pitch, yaw, roll);
}
public static Vector3 GetPitchYawRollDeg(Quaternion rotation)
{
Vector3 radResult = GetPitchYawRollRad(rotation);
return new Vector3(radResult.x * Mathf.Rad2Deg, radResult.y * Mathf.Rad2Deg, radResult.z * Mathf.Rad2Deg);
}
You should accept your answer as correct. It helps others find the solution easier :)
Your answer
Follow this Question
Related Questions
Rigidbody spins out of control when colliding into corner 0 Answers
use quaternions in unity to rotate object. 1 Answer
Min-Angle Constraint Script Causes Jitter 0 Answers
How can I turn my cube object to the direction it is traveling? 1 Answer
Rotating a Vector direction by another Vector direction in one axis 1 Answer