- Home /
Getting each axis angle from a game object
Is there a way to find out what is the angle of each and every single axis of a game object(transform.up,transform.right,transform.forward) and then reconstruct it with Quaternion.AngleAxis()?I see that there is a transform.ToAngleAxis(), but it seems that it converts the whole rotation to angle axis representation and i need each individual axis angle.
I may be wrong but you could try this along the different axes. look rotation use the normals from transform.right,up etc. again this might not work, but it would be my first port of call.
I'm not 100% certain why you would need this but, if I understand your question correctly, you want to split the rotation into its 3 component steps?
The trouble with saying "what is the angle of each and every single axis of a game object" is that the "angle" doesn't really make a lot of sense without an axis to go round, so it becomes a little ambiguous.
The Euler Angles (transform.eulerAngles or transform.localEulerAngles -- the numbers shown in the inspector) is a Vector3 representing an angle of rotation about each axis. For example, transform.eulerAngles = new Vector3(45f, 0f, 0f); is a rotation of 45 degrees around the x-axis (right). So these may be the angles you want.
However, be aware that order matters in rotations. Rotating about x then about y, will give you a different outcome than if you rotated about y then about x so watch out! You should also avoid using Euler Angles directly too much since they run into problems that Quaternions don't have (Unity does everything in quaternions internally anyway).
Another solution, if you're treating it as a step-by-step rotation, could be to use Quaternion.FromToRotation(Vector3 fromDirection, Vector3 toDirection), and split it up that way, recording the quaternion of each step.
Could you perhaps give a bit more detail on why you want to do this and what you want to achieve? It will make it easier to give a relevant solution.
Thanks =).
For an example of the last method, if you wanted to know the rotation from Vector3.right to transform.right (and so on), you could do:
Quaternion rotatedRight = Quaternion.FromToRotation(Vector3.right, transform.right);
Quaternion rotatedUp = Quaternion.FromToRotation(Vector3.up, transform.up);
Quaternion rotatedForward = Quaternion.FromToRotation(Vector3.forward, transform.forward);
Then each of those quaternions could be converted to an angle axis using Quaternion.ToAngleAxis(). However, you can likely just use them as they are (the angles are mostly there just to help us get our heads around things, everything done with quaternions at the end of the day).
Euler angles gives the angle of the current rotation yes, but i was wondering can i get this angle and construct it with Quaternion.AngleAxis().Ill spare the details because i am doing an overall tests with rotations, nothing serious.
If you take a look at this answer:
http://answers.unity3d.com/questions/696271/how-create-modern-aircrafts-hud.html
You can see code where I've deconstructed heading, pitch, and roll from the forward, up, and right of an object. It works fine for a plane HUD, but I suspect a bit more would need to be done to have a general solution.
Note in your test, while not related to the direction vectors, you can construct rotations based on eulerAngles. Given that 'v' is a Vector3 representing eulerAngles, you can construct that same rotation by:
rotation = Quaternion.AngleAxis(v.y, Vector3.up) * Quaternion.AngleAxis(v.x, Vector3.right) * Quaternion.AngleAxis(v.z, Vector3.forward);
Note that you must compose the rotations in the order specified here. Change the order that the Quaternions are composed, and the rotation of the eulerAngles will not match the physical rotation of the objdct.
Your answer
