Comments and answers for "Calculate Angle: From & To"
http://answers.unity.com/questions/456917/calculate-angle-from-to.html
The latest comments and answers for the question "Calculate Angle: From & To"Answer by Griffo
http://answers.unity.com/answers/456944/view.html
T$$anonymous$$s is what I use, but it only gives 0 to 180, 0 being directly in front 180 being directly be$$anonymous$$nd so 90 is both right and left, but I'm sure you can work out if the target is on it's right or left.
I only use it to allow the weapons to fire if wit$$anonymous$$n a certain angle.
var enemy : Transform;
private var targetDir : Vector3;
private var forward : Vector3;
private var targetAngle : float;
function Update(){
targetDir = enemy.position - transform.position;
forward = transform.forward;
targetAngle = Vector3.Angle(targetDir, forward);
}Wed, 15 May 2013 15:39:50 GMTGriffoAnswer by robertbu
http://answers.unity.com/answers/456925/view.html
Quaternion.Angle() and Vector3.Angle() give you back an unsigned angle. So you will need to figure out if the object is to the left or right of the helicopter. There are several posts with different solutions for figuring out if an object is to the left or right. Here is one:
[http://answers.unity3d.com/questions/13033/how-to-determine-if-enemy-is-on-my-left-or-right-h.html][1]
Your second problem is one you mention, the 'Y' height. Quaternion.Angle() and Vector3.Angle() will calculate the angle in 3D space. The typical way to solve t$$anonymous$$s is to modify the vectors to remove 'Y' (and use Vector3.Angle()). So your angle would be somet$$anonymous$$ng like:
Vector3 v3A = target.position - transform.position;
v3A.y = 0;
Vector3 v3B = transform.forward;
v3B.y = 0;
float angle = Vector3.Angle(v3A, v3B);
An alternate, you can approach t$$anonymous$$s problem as a 2D rotation, and use Atan2().
Vector3 v3A = target.position - transform.position;
float angle1 = Mathf.Atan2(v3A.z, v3A.x);
float angle2 = Mathf.Atan2(transform.forward.z, transform.forward.x);
float angle = Mathf.DeltaAngle(angle1, angle2);
[1]: http://answers.unity3d.com/questions/13033/how-to-determine-if-enemy-is-on-my-left-or-right-h.htmlWed, 15 May 2013 15:11:49 GMTrobertbuComment by raimon.massanet on raimon.massanet's answer
http://answers.unity.com/comments/456924/view.html
Wed, 15 May 2013 15:05:36 GMTraimon.massanetAnswer by raimon.massanet
http://answers.unity.com/answers/456920/view.html
You could do somet$$anonymous$$ng like t$$anonymous$$s, using trigonometry:
Vector3 toTarget = (target.transform.position - transform.position).Normalize();
Vector3 forward = transform.forward;
float angle = Mathf.Acos(Vector3.Dot(toTarget, forward));
But I'm sure there are more efficient ways of calculating it.Wed, 15 May 2013 14:54:15 GMTraimon.massanet