- Home /
Vector3.Angle getting twitchy with small angles
I'm trying to figure out the difference between a ship's heading and the direction of it's target (For AI purposes).
I use Transform.InverseTransformPoint(target.position) to get the location of the target in local space, then pass that into a Vector3.Angle, but the angle never drops below about 8 degrees, even when both target point and ship are exactly in line.
Hmmm...using Vector3.Angle(localSpaceTarget, Vector3.forward)
? Since, in its own local coords, the chasing ship is always at 000 facing north.
I'd print the local coords, just to be sure X is almost 0 when aimed at it.
I don't know if this will work, but maybe try taking the dot product of the target's transform.forward with your transform.forward. Since both are in world coordinates and already normalized, it should be fairly straight forward (no pun intended). so cos(theta) = (A dot B)/($$anonymous$$agnitude of A * $$anonymous$$agnitude of B). Since magnitudes are 1.0 of both, that means theta = acos(A dot B) or:
float theta = $$anonymous$$athf.Acos(Ax * Bx + Ay * By + Az * Bz);
I think that's right, don't have time to verify, but if it works, I'll move it to an answer.
nat: the OP was a little vague on top with "direction of it's target," then below clarified with "exactly in line." I think wants to aim player at the target, so dot prod of player forward and line to target -- (targ.position-player.position).normalized
.