Comments and answers for "Math help if possible, angle calculation"
http://answers.unity.com/questions/407478/math-help-if-possible-angle-calculation.html
The latest comments and answers for the question "Math help if possible, angle calculation"Comment by Fattie
http://answers.unity.com/comments/407512/view.html
you haven't learned everything about trig in high school math ?
that' amazing
I urge you to (A) leave a note at your school "You are all idiots, goodbye" (B) do nothing but study math, hard, for three months. you will then know everything about math.
unless you are incredibly good at math, you have very little power in the world. math is power. Good luck!Wed, 27 Feb 2013 11:36:07 GMTFattieAnswer 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