Answers for "Math help if possible, angle calculation"
http://answers.unity.com/questions/407478/math-help-if-possible-angle-calculation.html
The latest answers for the question "Math help if possible, angle calculation"Answer by fafase
http://answers.unity.com/answers/407497/view.html
I would do it like this:
public Transform target;
float ang;
void Update () {
float angInCos = Vector3.Dot(transform.forward,(target.position - transform.position).normalized);
ang = Mathf.Acos(angInCos);
ang = ang *Mathf.Rad2Deg;
Vector3 cross = Vector3.Cross(transform.forward,(target.position - transform.position).normalized);
if(cross.y<0)ang *=-1;
print (ang);
}
Dot returns the cosin of the angle which is between -1 and 1. 0 being 90 degrees.
The arcos of the dot returns the angle in radians, multiplying by the rad2deg you get it in degrees.
But, you would notice that you never get negative values, which could be fine for you, but then if you need to know whether it is right or left you use the Cross product which returns a vector3. Checking the value of the y component, you know if the agle is positive or negative.
I chose here, if cross.y is negative then your angle is negative and the target is on the left of the player.Wed, 27 Feb 2013 11:19:06 GMTfafase