- Home /
Get local euler angles by Quaternion
What do I need that is to get object's local euler angles by given global rotation. One solution is to set object required rotation and then roll back, but it seems ugly:
static Vector3 GetLocalEulerAtRotation(this Transform transform, Quaternion targetRotation)
{
var tempRotation = transform.rotation;
transform.rotation = targetRotation;
var localEuler = transform.localEulerAngles;
transform.rotation = tempRotation;
return localEuler;
}
Is there any another more compact and more optimized solution?
Answer by Bunny83 · Oct 15, 2017 at 02:31 AM
You seem to abuse a child object as temporary conversion object while you actually want to access the rotation of the parent object. The local eulerangles define the rotation of the transform in the parents coordinate space. So if your targetRotation quaternion is an absolute / worldspace rotation all you need is the inverse of the parents transform
So to replace your current code you can just do:
static Vector3 GetLocalEulerAtRotation(this Transform transform, Quaternion targetRotation)
{
var q = Quaternion.Inverse(transform.parent.rotation) * targetRotation ;
return q.eulerAngles;
}
It would make more sense to use the transform as local space instead of the parent of the object
Your answer
Follow this Question
Related Questions
Rotating an object around the centre of its mesh, using Quarternion AxisAngle 1 Answer
How to convert quaternion to vector3 with specific multiplication order? 5 Answers
Duplicating rotation with axis constraints 1 Answer
Rotating player with Quaternion and RotateTowards 1 Answer
Get player orientation, but only one Euler angle is non-zero 2 Answers