- Home /
Solved.
Aligning child's axis to parent's axis - world space: Why does my Quaternion.Angle never return values close to zero when they visually are?
I am aligning an object's +z axes - which points downwards - to the parent's -y axis which points downwards. That is, I want the child object to face down all the time, in respect to parent's object down.
It works well by using the following code:
// Obtain local forward vector relative to world
Vector3 localForward = this.child.transform.forward;
Debug.DrawRay(this.child.transform.position, localForward, Color.blue); // draw axes local space
Debug.DrawRay(this.child.transform.position, -this.parent.transform.up, Color.green); // draw down axes parent space
// Building the world (not-local) rotation from child's local z-axis to parent's y-axis
Quaternion verticalDown = Quaternion.FromToRotation(localForward, -this.parent.transform.up) * this.child.transform.rotation;
// apply it
this.child.transform.rotation = Quaternion.Slerp(this.child.transform.rotation, vertical, Time.deltaTime * 1.0f);
It works well. However, in the default pose (child object's forward is down and parent's down is down) the following never equates to zero, but 89-91.
Quaternion.Angle(this.child.transform.rotation, verticalDown);
Why is this so? I'd expect the value would be zero since both child's axes forward is facing down and parent's down is down in the beginning.
Look down.
Then, rotate 90 degrees to your right.
Your second rotation differs from your first by 90 degrees, but is still pointing in the same direction.
Any chance something like that is happening here?
Follow this Question
Related Questions
quaternion - rotate camera based on original facing at scene start? 0 Answers
Rotating clockwise or counter clockwise using Quaternions and PID 0 Answers
Why is my Quaternion rotation going crazy? 1 Answer
Unity.Mathematics equivalent to Quaternion.FromToRotation? 3 Answers
How to Increase Height of Pendulum? 1 Answer