Quaternion.... angles?

Started by
0 comments, last by NumberXaero 12 years, 2 months ago
I'm working in Unity and I'm trying to get an enemy to turn a certain way. Doing it with Euler angles is just not working and using quaternions works better, but still isn't quite there. Basically, imagine an enemy standing up straight and rotating him so the top of his head points at a desired position. This is only on 2 axis, so to get the angle I want to rotate I do


vec = targetPos - position;
vec = vec.normalize;
dotProd = Vector3.Dot((Vector3.up), vec);
angleToTarget = Mathf.Acos(dotProd) * (180/Mathf.PI);


So now I have the angle I want to rotate. I want to do this rotation about the X axis, since that is the axis that goes into the screen for me. How would I go about modifying the existing quanternion that is being used for rotations (which is just the identity quaternion) so it will rotate the object for the calculated angle about the X axis?
Advertisement
Try a Quaternion from axis angle. You have the angle, you know you want to rotate about the x axis.

angleRad *= 0.5
q.w = cos(angleRad);
q.v = nrmAxis * sin(angleRad)

then combine this quaternion with the existing one, and use the result.

This topic is closed to new replies.

Advertisement