- Home /
Vector3.Angle noob question
who can explain why:
Debug.Log (Vector3.Angle(Vector3.up,new Vector3(-1,-1,0)));
Debug.Log (Vector3.Angle(Vector3.up,new Vector3(1,-1,0)));
both return same value 135, instead of 135 and 235. (The same with Vector2) and how to get around that?
PS the very short answer is "it calculates the angle BETWEEN two vectors" -- the shortest path, not the angle "from one to another" in a certain direction. Long answer below.
Answer by Fattie · Jan 17, 2012 at 12:57 PM
Say you are standing up and looking ahead.
(-1,-1,0) is LEFT and DOWN ... at a 45 degree angle. Ok?
imagine a "tunnel" going in to the ground, to your LEFT, at a 45 degree angle.
(1,-1,0) is RIGHT and DOWN ... also at a 45 degree angle. Ok?
imagine a "tunnel" going in to the ground, to your RIGHT, at a 45 degree angle.
In fact! from BOTH those places, (from both the tunnels) to get UPRIGHT ... up to the sky ... is 135 degrees!
it makes no difference what "compass direction" the tunnel is pointing.
Can you see this OK now ?
Next: you ask about "235". I'm assuming you want it to go "all the way around" is that correct? "Vector3.Angle" won't help you with that because of course it goes the 'shortest way".
What you are talking about is actually a very difficult problem in game programming! But I suggest the simplest solution for you is, very simply in your example 360 minus 135 is 235. Just check if you are "on the right" and then you know you have to say 360 minus the number. You see what I mean?
if that doesn't work for you (or anyone reading in the future) your next step is to learn about Quaternions, and beyond that a lot of trigonometry and beyond that just more logic like "if I am down and to the right, I need to do THIS calculation ..." Its pretty annoying at any level!
References say: "Returns the angle in degrees between from and to." It suggested to me that it matters which one is from and which one is to and returns from - to. If it said just between, I would understand the shortest way which is the answer to my question.
You are absolutely correct, $$anonymous$$e - the documentation could be clearer. I agree with you completely. Hope it helps!
Answer by aqez · Mar 04, 2012 at 02:03 AM
I found this while searching for the solution to this problem..
http://www.gamedev.net/topic/542870-determine-which-side-of-a-line-a-point-is/
This solved my issue, the final code looking something like..
Vector3 difference = heading - p.ShipInfo.Position;
Vector3 perp = new Vector3(-difference.y, 0, difference.z);
float d = Vector3.Dot(hit.point - p.ShipInfo.Position, perp);
Debug.Log(Mathf.Sign(d));
float degrees = Mathf.Acos(Vector3.Dot(heading.normalized, (hit.point - p.ShipInfo.Position).normalized)) * Mathf.Rad2Deg;
if (Mathf.Sign(d) > 0)
degrees += 180;
Debug.Log(degrees);
Side note, I work in x,z coordinates instead of x,y so replace where necessary.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063106im_/https://answers.unity.com/themes/thub/images/avi.jpg)