An Unexpected problem
I want to start off by apologizing about missing my post on Monday, I missed work because I wasn’t feeling well, and it completely slipped my mind.
Anyway in the last week I ran into an interesting problem that I didn’t actually expect to run into. The problem is: “Rotating towards an entity along a single axis”.
I did a bunch of variations of math using quaternions and suggestions from the forums and none of them seemed to truly work the way I wanted. So I deleted all my code and came up with a method using Quaternion.LookRotation along with using the Dot Prodcut to find the pitch. I’m sure there are better ways to do this, but not knowing all the ins and outs of Vicious this is the best one I could find. What I really wanted to do is to project the vector to the target onto the forward vector and then perform a dot product between the two normalized vectors, first determining if the object is in front or behind the turret. If in front I would know the change in that angle, if behind I could very easily begin rotating to resolve the angle. For some reason though, when assigning the euler angles with this math ( which I was 99% sure was correct ) the turret would spin uncontrollably. I consider myself very good at vector mathematics, but still very new understanding how this would all work in Unity. I also read a suggestion about using LookAt and then zeroing the unneeded angle rotations, but this resulted in even more sporadic behavior.
What I have since figured out is that the forward vector of the turret actually was rotating with the child node I was changing the rotation on. What this was causing is the constant spinning. I think I could of made my initial math work great if I just used the world forward instead of the mesh forward. So my math before was correct just missing one very small piece of the puzzle. The assumption I had made was that the forward vector of the parent of the node would remain stationary, but in fact this wasn’t the case, so my 99% of certainty was indeed wrong. Without serious testing its hard to figure out the exact cause for the change in rotation of the forward vector of the parent, was it an animation, was it the child effecting, could it of been something with the way it was exported, or the way the hierarchy was set up, or maybe it was just an error in my code.
Needless to say I wasted quite a few hours doing what I thought to be a trivial task, and here is the code that I ended up with.
Vector3 toTarget = newTarget.transform.position – transform.position;
Quaternion quat = Quaternion.LookRotation( toTarget,Vector3.up );
Vector3 eulerFinal = new Vector3( transform.eulerAngles.x, quat.eulerAngles.y + 90.0f, transform.eulerAngles.z );
transform.eulerAngles = eulerFinal;
Transform tilt = transform.Find( “D_TurretPivot” );
eulerFinal = new Vector3( tilt.eulerAngles.x, tilt.eulerAngles.y, ( Mathf.Rad2Deg * Mathf.Acos( Vector3.Dot ( toTarget, Vector3.up ) ) ) – 90.0f );
tilt.eulerAngles = eulerFinal;
Debug.Log( eulerFinal );
Thanks for reading, and until next time.