Answers for "Angle between two objects' facing direction"
http://answers.unity.com/questions/249047/angle-between-two-objects-facing-direction.html
The latest answers for the question "Angle between two objects' facing direction"Answer by Mehrdad995
http://answers.unity.com/answers/1378972/view.html
I personally found this more simple and understandable
public static float CalculateAngle180_v3(Vector3 fromDir, Vector3 toDir)
{
float angle = Quaternion.FromToRotation(fromDir, toDir).eulerAngles.y;
if(angle > 180){return angle - 360f;}
return angle;
}
you can also return all angles as a vector3Fri, 14 Jul 2017 14:24:48 GMTMehrdad995Answer by Piflik
http://answers.unity.com/answers/249057/view.html
Vector3.Angle gives you the angle between two directions. Just put transform.forward of both objects in there, and it should give you the angle you need.
It has one limitation, though, it doesn't distinguish between CW and CCW. I wrote a function for that:
function angleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
var perp: Vector3 = Vector3.Cross(fwd, targetDir);
var dir: float = Vector3.Dot(perp, up);
if (dir > 0.0) {
return 1.0;
} else if (dir < 0.0) {
return -1.0;
} else {
return 0.0;
}
}
It returns 1.0 for CW, -1.0 for CCW and 0.0, if the angle between the two objects is 0.Sun, 06 May 2012 18:08:16 GMTPiflik