Hello!
I’m trying to rotate an object so it always stays parallel with a direction-vector, it works fine besides when the vector points down ~170 degrees, then the object spins randomly around the Y-axis.
The object has no parent and its rotation and position is set to zero. It looks just like gimbal-lock but still I do the rotation with only one Quaternion. I have now tried many different ways to do this but they all end up having the exact same problem!?
I could compensate the local Y orientation with an Quaternion in the next step, but how can I read it’s current local Y and only the Y as an Quaternion? I need to read it to reset it to zero. (The local Euler value can’t be used here, it just brings more rotation chaos.)
This must be a well known problem, so is there any known solution?
(How to stop the random spinning…?)
// The direction-vector pointing at the target:
Vector3 vectorToTarget = Target.transform.position.normalized;
// First try: using -> FromToRotation()
this.transform.rotation = Quaternion.identity; // Start with no rotation
this.transform.rotation = Quaternion.FromToRotation(this.transform.up, vectorToTarget)*this.transform.rotation;
I have also tried with:
// Second try: using -> AngleAxis()
// Get the angle between the up-vector and vectorToTarget
float angle = Vector3.Angle(Vector3.up, vectorToTarget);
this.transform.rotation = Quaternion.identity; // Start with no rotation
this.transform.rotation = Quaternion.AngleAxis( angle, Vector3.Cross(Vector3.up, vectorToTarget).normalized ) * this.transform.rotation;
And also:
// Third try: using -> RotateAround()
// Get the angle between the up-vector and vectorToTarget
float angle = Vector3.Angle(Vector3.up, vectorToTarget);
this.transform.rotation = Quaternion.identity; // Start with no rotation
this.transform.RotateAround(Vector3.zero, Vector3.Cross(Vector3.up, vectorToTarget).normalized, angle)
Thanks in advance! ![]()