Good day all.
I have a 3d object with a random rotation. I can express that object's rotation both through a quaternion or through an euler rotation.
Then I have a vector3 normal that I get from a raycast.
What I need, is to apply a rotation that will make a chosen axis of the 3d object, coincide with the normal vector3 from above.
The closest I got to the solution is this (trying to align the 'up' vector):
Quaternion q;
Vector3 a = Vector3.Cross(3dobject.up, varnormal);
q.x = a.x;
q.y = a.y;
q.z = a.z;
q.w = Mathf.Sqrt((3dobject.up.sqrMagnitude) * (varnormal.sqrMagnitude))
+ Vector3.Dot(3dobject.up, varnormal);
3dobject.rotation = q * 3dobject.rotation;
But when the normal is negative on the y (e.g. 0,-1,0), the object aligns with its y pointing up instead.
What's wrong with the formula? Is there a better solution maybe?
Thanks in advance.