- Home /
Math help if possible, angle calculation
Hi, Am wondering if anyone could help me with some math if possible. Am trying to calculate the angle differency between my "vision" (Camera Y) and direction between objectPosition - myPosition, I got help with this math so am not 100% sure if I know it outside in(my school math havent cover this section yet/or if it even will), from a little googling I found this http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm with angle = acos(v1•v2) and I also found http://answers.unity3d.com/questions/119313/Calculating-the-Angle-between-two-vectors.html Even thou I remade my formular exactly the same it still won't work. my camera won't get any different value except 1 after the normalizing,although I get the 0>360 value from the rotation. I am currently using the first person unity standard script that is NOT modified by any means.
But I know I get the correct input from the rotation and it does indeed change. I know that normalizing is suppose to "minimize" vector into a |1| direction vector.(I think) Help and some explanation would be awesome >.<
I tried to do this after I googled. My viewDirection always stays 1 after normalizing,
viewDirection = transform.eulerAngles;
objectDirection = transform.position;
viewDirection.Normalize();
objectDirection.Normalize();
d = Mathf.Acos(Vector3.Dot(viewDirection,objectDirection));
d *= Mathf.Rad2Deg;
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!
Answer by fafase · Feb 27, 2013 at 11:19 AM
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.
Your answer
Follow this Question
Related Questions
Lerp Color between 4 corners 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
C# How Add new score to old score in update function 2 Answers