How do I find the rotation of a game object in relation to its grandparent
I have 5 objects each rotated randomly along multiple axes, parented as such:
rootObject -> gimbleObject -> parentObject -> mainObject -> propObject
I can obviously find the rotation of the propObject in relation to it's parent, the mainObject — using transform.localEulerAngles. I can also find the rotation of the propObject in relation to 'the world / scene' — using transform.rotation.eulerAngles.
But is there a quick way to return the Euler angles of my propObject in relation to other objects in the scene? — such as the angle of the propObject in relation to the gimbleObject? In other words: is there a quick way of returning the rotation of a game object in relation to its grandparent?
I guess another way of looking at this problem is: is there a way of returning the Euler angles based on a specific up and forward vector?
Answer by Al_Goyle · Feb 09, 2016 at 08:29 AM
What about: (untested)
Quaternion localRotation = Quaternion.Inverse(propObject.transform.rotation) * gimbleObject.transform.rotation;
Or... possible workaround:
float relativeAngle = Vector3.Angle(propObject.transform.forward, gimbleObject.transform.forward);
Also, considering your last sentence... Quaternion.LookRotation returns a rotation given a forward and up vectors I think.
Your answer
Follow this Question
Related Questions
Rotate LOCAL y axis to match Joystick input 0 Answers
Help with raycast for for wall detection 1 Answer
Changing parent at runtime rotates object. 0 Answers
How to rotate the player towards a Vector2? 1 Answer
I want to make a script that causes the directional light to stop at 180 degrees. 0 Answers