- Home /
Vector3.Angle returning wrong values for vectors with small components
I tried to get the angle between two Vector3's using Vector3.Angle and got a return value I didn't expect.
The two vectors where (1, 0, 0)
and (0.00001, 0, 0)
and the angle between the two should obviously be 0
. However, Vector3.Angle
returns 90
.
This isn't related to the length difference of the vectors, Vector3.Angle
also reports the angle between (0.00001, 0, 0)
and itself as 90. On the other hand, the difference between (1000000, 0, 0)
and itself or (1, 0, 0)
is reported correctly as 0
.
Is this related to the algorithm used by Vector3.Angle
or a bug? The documentation doesn't say anything about it expecting only vectors in a specific range or only normalized ones.
Answer by Adrian · Aug 26, 2011 at 11:24 AM
Normalizing probably is the issue here. The documentation on Vector3.Normalize 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.
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 vector
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"));
Answer by elliselkins · Apr 21, 2016 at 08:23 PM
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 for more details.
Ok, but something going wrong: A & B is a Vector2. if we do this $$anonymous$$athf.Atan2(B.y - A.y, B.x - A.x) * 180 / $$anonymous$$athf.PI then we got that A (1.0, -1.0) B (-1.0, 1.0) = 135
???
$$anonymous$$athf functions return polar coordinates ins$$anonymous$$d of game coordinates.
But the angle between A (1.0, -1.0) B (-1.0, 1.0) = 180
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.
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.
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
Your answer
Follow this Question
Related Questions
Simple vector math help. 1 Answer
How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer
Result of subtracton vectors 1 Answer
What is the 3rd point of Vector3.Angle ? 1 Answer
Angle between two lines in 2D 3 Answers