I want to be able to find the negative angle similar to Vector3.Angle.
I want this for a hit detection gui that rotates in the direction of the enemy that is shooting the player. Vector3.Angle works, but it will only work one way (it will rotate to the right, but not left)

I had this problem once too: Vector3.Angle returns an absolute value. You can use a cross product to know the angle polarity - the cross product of vectors A and B results in a vector C perpendicular to A and B, but pointing to one side if A is “before” B, and to the other if A is “after” B. If vectors A and B are roughly in the horizontal plane, for instance, you can use the polarity of C.y as the angle signal:

    var angle:float = Vector3.Angle(vectorA, vectorB);
    var cross:Vector3 = Vector3.Cross(vectorA, vectorB);
    if (cross.y < 0) angle = -angle;

Maybe you need to invert the angle or swap vectorA and vectorB to get the polarity you expect.

Now done natively: Unity - Scripting API: Vector3.SignedAngle

Sure. :slight_smile:

What you are interested in is actually the orientation of the vectors with respect to one another; that is, you want to know if, given vectors 1 and 2, does vector 2 lie to the left or the right of vector 1? If it’s to the left, you consider the angle between them to be positive, and contrarily, if it is to the right, you consider the angle to be negative.

Crossing the vectors is one way to determine this, but you have to project the vectors onto some plane of reference first. Let’s say you have the two vectors v1 = (1,0,0) and v2 = (0,1,0). It’s obvious to everyone that the angle between them is positive 90 degrees because v2 is “to the left” of v1, right? But that’s only when viewing their projection onto the z-plane, i.e. dropping their z-coordinate and considering only (1,0) and (0,1). If viewed from the y-plane, the angle stops making sense at all because one of the vectors becomes the zero-vector.

So, before you do any kind of testing to determine their orientation with respect to one another, you have to decide on some plane of reference within which their angles are to count. Let’s say I decide to perform this test with their projections onto the z-plane. Then, calculate the cross product. If the cross product’s z value is positive, it means the angle between them is positive as well. If the cross product is negative, it means the angle is negative. Consider this piece of code:

Vector3 v1 = new Vector3(1, 0, 0);
Vector3 v2 = new Vector3(0, 1, 0);

private void Start()
{
    int sign = Vector3.Cross(v1, v2).z < 0 ? -1 : 1;
    Debug.Log(sign * Vector3.Angle(v1, v2));
}

This prints 90, and -90 if you swap the two vectors. Make sense?

it is a really old question but Google sent me here as first option so it may happen with someone else in the future, in my case I was trying rotate something to the left Vector3(0, -90, 0) wasn’t working so I looked everywhere trying to find a solution and nothing helped me, it was simple as put it as a float: Vector3(0, -90f, 0) it turn 90 degrees to left now.

transform.eulerAngles.x (y or z) ?