- Home /
Vector3.Angle
I would have thought that the follow two debug lines would return 90 & -90 (270) but they are both returning 90. Why is this?
Debug.Log(Vector3.Angle(new Vector3(1,0,0), new Vector3(0,1,0)));
Debug.Log(Vector3.Angle(new Vector3(-1,0,0), new Vector3(0,1,0)));
I assume the function is working correctly, so how would I get the desire result?
Answer by whydoidoit · Jul 09, 2012 at 07:09 PM
The angle between two vectors is always between 0 and 180 degrees. It is the angle between them and not a rotation. The angle is calculated using the dot product. See this thread for testing left and right.
The cross product is creating a perpendicular vector - depending on whether one side is greater than the other the vector is pointing upwards or downwards. So taking the dot product of that vector against up tells you the basic orientation of the vectors. The dot product being >= 0 implies that they are pointing in the same direction and the first vector is smaller than the second.
Answer by Brian Stone · Jul 09, 2012 at 07:42 PM
Mike nailed it.
If you want to know the direction that one vector is turned relative to another (i.e. clockwise or counter-clockwise) on a reference plane, then you can compare the cross product of the two vectors and compare the resulting vector's direction to the reference plane's normal vector.
Given two vectors A and B (representing rotation from vector A to vector B) and given the normal vector of a reference plane, N. Using the right-hand-rule of turning with N acting as the axis of rotation, the direction of turn is given by the following algorithm:
// The direction of turn from vector A to vector B relative to N.
float direction = Mathf.Sign(Vector3.Dot(Vector3.Cross(A, B), N));
If direction > 0, then the turn is clockwise.
If direction < 0, then the turn is counter-clockwise.
Your answer