0
votes

I am using in Unity the function

Quaternion.LookRotation(normal, (0,1,0));

The docs you can find here https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html. This should be a problem, if the normal is parallel to the y-axis. Because the coordinate system created by the function cannot be created and so the Orientation of the resulting Quaternion is spinning around the y-axis So I try to cover this edge case by using

if(Vector3.Angle(normal, new Vector3(0, 1, 0)==0) ||Vector3.Angle(normal, new Vector3(0, 1, 0)==180)  )

After Debugging I realized that the problem is occuring for each Vector3.Angle(normal, new Vector3(0, 1, 0) between 170 and 180. I need to get a really precise orientation, so my question is do you know why this inaccuraccy is happening and how it can be properly handled. Thank you !

1

1 Answers

0
votes
float t = Mathf.Abs(Vector3.Dot(normal.normalized,Vector3.up));
Quaternion a = Quaternion.LookRotation( normal , Vector3.up );
Quaternion b = Quaternion.LookRotation( Vector3.up , -Vector3.forward );

Quaternion rot = t<0.99f ? a : b;

Where 0.99 will translate here to a hard threshold of 0.9 degree (1-0.99)*90deg

OR try interpolating smoothly between them:

Quaternion rot = Quaternion.Slerp( a , b , Mathf.Pow(t,10) );