Comments and answers for "Vector3.Angle returning wrong values for vectors with small components"
http://answers.unity.com/questions/160514/vector3angle-returning-wrong-values-for-vectors-wi.html
The latest comments and answers for the question "Vector3.Angle returning wrong values for vectors with small components"Comment by juanitogan on juanitogan's comment
http://answers.unity.com/comments/1623384/view.html
Bunny83 answers this very well below, but here's another explanation:
<br><br>
The Atan2 function is an old tool typically used in a polar coordinate system for finding the polar direction from 0,0 to the given point in space. It existed long before Unity's Vector structs and Unity's coordinate system.
<br><br>
Unity's Vector structs are typically defined EITHER as isolated points in space, OR as vectors that move from 0,0 (or 0,0,0) to the given point.
<br><br>
To complicate things further, polar coordinates defines the 0-degrees direction as east, and Unity defines the 0-degrees direction as north.
<br><br>
So, yes, if we are talking about two different Unity vectors (which is what the OP is also talking about) -- one from 0,0 to 1,-1, and the other from 0,0 to -1,1 -- then the angle between those two vectors is indeed 180 deg.
<br><br>
But, if instead we are talking about two points in space, and are looking for the polar direction from 1,-1 to -1,1 (or 0,0 to -2,2, after the math applied in elliselkins' functions), then the answer is 135 deg (a.k.a. NW, or 45 deg in Unity bearings).
<br><br>
Thus, my point is that if you mix Mathf functions (which are adapted from things non-Unity) with Unity's Vector and/or Quaternion functions -- without being fully aware of the differences between the purposes of them -- you're in for a world of hurt. It also helps to keep the concepts of angles between objects, bearings of a vector or direction, and points in space, separate from each other (which is where this "Answer" completely fails and only makes the conversation more confusing).Thu, 18 Apr 2019 06:25:56 GMTjuanitoganComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1623095/view.html
This answer may be misleading since it expects two 2d position vectors as inputs, not two direction vectors like Vector3.Angle. This method here also just calculates the mathematical counter clockwise angle between the worldspace x axis and the connection vector between the two points. This is fundamentally something different.
<br>
The result of 135 is right for the two point A=(1.0, -1.0) and B=(-1.0, 1.0). The actual direction vector will be (-2, 2) which is 135° away from the x axis.
<br>
Of course as i said this answer is providing a solution for a completely different / unrelated problem.
***edit***
Note that Vector3.Angle actually expects two direction vectors and it's [implemented like this][1]
[1]: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector3.cs#L295Wed, 17 Apr 2019 11:34:03 GMTBunny83Comment by VirtualSUN on VirtualSUN's comment
http://answers.unity.com/comments/1622940/view.html
But the angle between A (1.0, -1.0) B (-1.0, 1.0) = 180Tue, 16 Apr 2019 23:57:59 GMTVirtualSUNComment by juanitogan on juanitogan's comment
http://answers.unity.com/comments/1622924/view.html
Mathf functions return polar coordinates instead of game coordinates.Tue, 16 Apr 2019 23:00:58 GMTjuanitoganComment by VirtualSUN on VirtualSUN's answer
http://answers.unity.com/comments/1573669/view.html
Ok, but something going wrong:
A & B is a Vector2.
if we do this
Mathf.Atan2(B.y - A.y, B.x - A.x) * 180 / Mathf.PI
then we got that
A (1.0, -1.0) B (-1.0, 1.0) = 135
???Wed, 21 Nov 2018 08:23:55 GMTVirtualSUNAnswer by elliselkins
http://answers.unity.com/answers/1174912/view.html
I ran into this problem as well, so I wrote my own angle functions.
//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 degress
public static float AngleInDeg(Vector3 vec1, Vector3 vec2)
{
return AngleInRad(vec1, vec2) * 180 / Mathf.PI;
}
See [Atan2][1] for more details.
[1]: http://docs.unity3d.com/ScriptReference/Mathf.Atan2.htmlThu, 21 Apr 2016 20:23:50 GMTelliselkinsComment by $$anonymous$$ on $$anonymous$$'s answer
http://answers.unity.com/comments/598148/view.html
thanks for that, was so disappointed with unity :)Mon, 16 Dec 2013 18:48:41 GMT$$anonymous$$Comment by chris.weigle on chris.weigle's answer
http://answers.unity.com/comments/598070/view.html
No, that's just due to the default ToString format for float.
Try:
Debug.Log ("Vector2.right * 0.049 = " + (Vector2.right * 0.049f).ToString("R"));Mon, 16 Dec 2013 16:48:20 GMTchris.weigleComment by $$anonymous$$ on $$anonymous$$'s answer
http://answers.unity.com/comments/598066/view.html
In case anyone else looking for more details I've found out that Unity zeros all vectors shorter than 0.05f.
Just try this: Debug.Log ("Vector2.right * 0.049 = " + Vector2.right * 0.049f); results in a zeroed vectorMon, 16 Dec 2013 16:42:41 GMT$$anonymous$$Answer by Adrian
http://answers.unity.com/answers/160517/view.html
Normalizing probably is the issue here. The documentation on [Vector3.Normalize][1] states:
> If the vector is too small to be normalized a zero vector will be returned.
`Vector3.Angle` probably normalizes the input vectors, calculating the angle between `(1, 0, 0)` and `(0, 0, 0)`. `Vector3.Angle` also seems to return `90` when either of its input vectors has zero length.
[1]: http://unity3d.com/support/documentation/ScriptReference/Vector3.Normalize.htmlFri, 26 Aug 2011 11:24:58 GMTAdrian