Comments and answers for "Angle between two vectors"
http://answers.unity.com/questions/317648/angle-between-two-vectors.html
The latest comments and answers for the question "Angle between two vectors"Comment by spiritLink on spiritLink's answer
http://answers.unity.com/comments/1539758/view.html
Thank you !Wed, 08 Aug 2018 09:39:40 GMTspiritLinkComment by bendangelo on bendangelo's answer
http://answers.unity.com/comments/1522859/view.html
This worked best for me.Wed, 27 Jun 2018 18:48:05 GMTbendangeloComment by elliselkins on elliselkins's comment
http://answers.unity.com/comments/1421447/view.html
Threw me for a loop for a minute too :).
This is because these functions measure the angle between the points, not the angle of 0,0 to these points. So take your first example, the first point is at 1,0, and the second point is at 0,1. Draw a line from the first point to the second. What angle did you have to draw that line? 135 degrees. If you drew 90 degrees then you'd be drawing straight up from the first point, but that's not where the second point is. If you want an answer of 90 degrees then this would do it: first point at 0,0, second point at 0,1.
Make sense?Mon, 16 Oct 2017 14:31:52 GMTelliselkinsComment by FedericaVigg on FedericaVigg's answer
http://answers.unity.com/comments/1420135/view.html
Hello,
I tried to use the code of @elliselkins, and I tested it with some vectors values, but is returning an incorrect value in this case:
float test2 = CalculateAngleBetweenVectors (new Vector3 (1f, 0, 0), Vector3.up);
return 135 instead of 90
and float test3 = CalculateAngleBetweenVectors (new Vector3 (-1f, 0, 0), Vector3.up);
return 45 instead of -90.
Any idea?
ThanksFri, 13 Oct 2017 13:29:29 GMTFedericaViggComment by AntzyP on AntzyP's comment
http://answers.unity.com/comments/1380131/view.html
Not picking a fight here, but just because it worked for your use case, it doesn't make it correct. It doesn't give the correct angle value(which is its primary function). Sure there are many ways to get the correct result but that function doesn't arrive at the correct result at all.
It is important to make the difference between "it works so it should be right" and "it is right so it should work". I thought it was latter since 2 comments said it worked spot on. But it didn't give the correct values and I wasted quite a bit of time on it. Luckily, I could figure out that this function was wrong and write my own code to get the correct values. A less experienced developer would think that this code is correct(since the community says so) and that something is wrong with his own code, and end up with a LOT of wasted time. The community is here to guide and help people, not lead them into frustration by claiming what is wrong as right.Mon, 17 Jul 2017 12:09:42 GMTAntzyPComment by Lance_JZ on Lance_JZ's comment
http://answers.unity.com/comments/1377480/view.html
Whatever works, is correct. You can do the same thing in different ways. I'm using it in my game AntzyP so yeah it works.
The top one is for the tank barrel. The bottom one is for the tank turret.
private float AngleTo(Vector2 pos, Vector2 target)
{
Vector2 diference = Vector2.zero;
if (target.x > pos.x)
diference = target - pos;
else
diference = pos - target;
return Vector2.Angle(Vector2.right, diference);
}
private float AngleTo(Vector2 pos, Vector2 target)
{
Vector2 diference = target - pos;
float sign = (target.y < pos.y) ? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}Tue, 11 Jul 2017 20:32:41 GMTLance_JZComment by AntzyP on AntzyP's comment
http://answers.unity.com/comments/1377195/view.html
How is fermmmm's script correct?
It returns the angle between difference of the vectors and right vector!
Assuming vec1 is Vector2.right and vec2 is Vecor2.up, the angle between them should be +90 degrees or -270 degrees(if anti-clockwise is positive rotation, as in case of 2D unity rotations about Z axis). But this function will give 135 degrees which is clearly incorrect.
The correct script is:
float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 vec1Rotated90 = new Vector2(-vec1.y, vec1.x);
float sign = (Vector2.Dot(vec1Rotated90, vec2) < 0) ? -1.0f : 1.0f;
return Vector2.Angle(vec1, vec2) * sign;
}Tue, 11 Jul 2017 09:31:53 GMTAntzyPComment by SquadMc on SquadMc's comment
http://answers.unity.com/comments/1375486/view.html
fermmmm's solution worked for me as it was important to have the positive and negative values for differentiation. Thanks for it!Fri, 07 Jul 2017 18:00:58 GMTSquadMcComment by Lance_JZ on Lance_JZ's answer
http://answers.unity.com/comments/1371913/view.html
fermmmm's solution is best. He should have posted that as a separate post. His works perfectly, whereas the one above does not work at all. I had the same issue.Thu, 29 Jun 2017 23:08:12 GMTLance_JZComment by Tokars on Tokars's answer
http://answers.unity.com/comments/1352420/view.html
This is the best solution that I have found, very convenient. Thanks!<br>
bullet.transform.rotation = Quaternion.Euler(0, 0, **AngleInDeg**(bullet.transform.position, target.transform.position));Fri, 12 May 2017 07:11:43 GMTTokarsComment by fermmmm on fermmmm's answer
http://answers.unity.com/comments/1202706/view.html
This is my solution that does not have the problem of the never negative angle and is useful to understand how to use Vector2.Angle.
private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = vec2 - vec1;
float sign = (vec2.y < vec1.y)? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}Tue, 14 Jun 2016 19:01:12 GMTfermmmmAnswer by elliselkins
http://answers.unity.com/answers/1174920/view.html
Ran into trouble with the Vector3.Angle function, it was returning incorrect values. Found out there is a problem with this function when the vectors are too small, as seen in [this question and answer][1]. So I wrote my own angle functions that use Mathf.Atan2:
//This returns the angle in radians
public static float AngleInRad(Vector3 vec1, Vector3 vec2) {
return Mathf.Atan2(vec2.y - vec1.y, vec2.x - vec1.x);
}
//This returns the angle in degrees
public static float AngleInDeg(Vector3 vec1, Vector3 vec2) {
return AngleInRad(vec1, vec2) * 180 / Mathf.PI;
}
See [Atan2][2] for more details.
[1]: http://answers.unity3d.com/questions/160514/vector3angle-returning-wrong-values-for-vectors-wi.html#answer-1174912
[2]: http://docs.unity3d.com/ScriptReference/Mathf.Atan2.htmlThu, 21 Apr 2016 20:31:17 GMTelliselkinsAnswer by Skibur
http://answers.unity.com/answers/523066/view.html
Since you are using two dimensional grid axis, I use Vector2. using Vector3 is nothing different than the one I wrote;
void Update(){
Vector2 PointA = new Vector2(z, y);
Vector2 PointB = new Vector2(z, y);
float Angle = Vector2.Angle(PointA, PointB); //If the angle isn't correctly at 0, you can subtract this value by the offset degree
Debug.Log("Angle of PointA to PointB is " + Angle);
}
This is done via C#.Sat, 24 Aug 2013 06:20:26 GMTSkiburComment by fafase on fafase's answer
http://answers.unity.com/comments/523063/view.html
You never get negative value because it is based on the cosine of the angle and looking at your trig circle, you will notice there is no -cos, it actually goes cos, sin, cos, -sin because cos = -cos. Rings a bell from high school? :)
So yep, going with the Cross product, you can see if the result vector goes up (pos) or down(neg) (conventional way to say as it does not necessarily goes up OR down, just one direction or the opposite). Then checking the direction tells you if the angle is pos or neg.Sat, 24 Aug 2013 06:01:23 GMTfafaseComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/523036/view.html
Yes, the result is exactly the same - that's similar to what Unity does internally to implement Vector3.Angle:
public static float Angle(Vector3 from, Vector3 to)
{
return Mathf.Acos(Mathf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
}
Since the cosine is symmetrical about 0, the angle is the same no matter if "to the left" or "to the right". If you want to know the side, the cross product may help you. Supposing that both vectors are roughly in the horizontal plane (XZ), the Y component of their cross product may define the sign - for instance:
function SignedAngle(a: Vector3, b: Vector3){
var angle = Vector3.Angle(a, b); // calculate angle
// assume the sign of the cross product's Y component:
return angle * Mathf.Sign(Vector3.Cross(a, b).y);
}Sat, 24 Aug 2013 04:49:57 GMTaldonalettoComment by nicloay on nicloay's answer
http://answers.unity.com/comments/523007/view.html
looks like is that result is exactly the same as in Vector3.Angle and it doesn't show negative valuesSat, 24 Aug 2013 03:52:04 GMTnicloayAnswer by fafase
http://answers.unity.com/answers/317703/view.html
@DarkMatter's answer is the solution for fast and easy solution with no possibility of error.
Now here the mathematics solution that lies under the Angle function in case you would need to stop on the way for any reason OR you want to make it hard on you:
var vec1=Vector3(2,3,4);
var vec2= Vector3(1,-2,3);
//Get the dot product
var dot:float = Vector3.Dot(vec1,vec2);
// Divide the dot by the product of the magnitudes of the vectors
dot = dot/(vec1.magnitude*vec2.magnitude);
//Get the arc cosin of the angle, you now have your angle in radians
var acos = Mathf.Acos(dot);
//Multiply by 180/Mathf.PI to convert to degrees
var angle = acos*180/Mathf.PI;
//Congrats, you made it really hard on yourself.
print(angle);Fri, 14 Sep 2012 05:55:54 GMTfafaseAnswer by DarkMatter
http://answers.unity.com/answers/317677/view.html
You can get the angle between two vectors using [Vector3.Angle][1](v3A, v3B)
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.html
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.htmlFri, 14 Sep 2012 04:55:54 GMTDarkMatter