- Home /

# Is there way to find a negative angle?

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)

**Answer** by aldonaletto
·
Nov 01, 2011 at 11:55 AM

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.

Damn it, ninja'ed by aldo. ;) +1 for being quicker in conveying the same idea. :P

Great answer, very concise! Works as a drop-in replacement for Vector3.Angle, with the expected polarity.

Nope. This is working on the assumption you're comparing forward and right (Z and X respectively). The cross product of those vectors is either up or down-facing, so it's based on whether the Y value for the cross product is positive or negative (as well as the order of variables in the cross product, but that's already accounted for)

Sir, I am facing a similar situation which I have mentioned in my problem here. It hasnt got any answers as of yet.

$$anonymous$$y problem isn't being solved by cross product. I need to detect a change in direction WHEN a body/gameObject in rotation changes its direction of rotation. Please Help !!

**Answer** by JoaoBorks
·
Jul 13, 2017 at 04:54 PM

Now done natively: https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

**Answer** by CHPedersen
·
Nov 01, 2011 at 12:14 PM

Sure. :)

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?

Good explanation! I ninja'ed you because this time my answer was more concise, what is really a rare thing! Conciseness isn't a quality of ours... (what's a good thing for the OPs)

**Answer** by dr4
·
Jul 05, 2016 at 06:09 AM

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.

**Answer** by DRP.du
·
Nov 01, 2011 at 12:23 PM

transform.eulerAngles.x (y or z) ?

This works for knowing the rotation of some object, but it seems the OP wants to know the angle between two vectors with Vector3.Angle(v1, v2) - and this function only returns an absolute value.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.