- Home /

# Find if a target point is left or right, and infront or behind a character. (Angle between three points?)

So I’m caving and asking a question :/

I have a character, and a target point. I need to find if the target is left or right, and in-front or behind the character. Accounting for the direction the character is facing. I’m sorry if this is duplicated somewhere, but after 20 odd youtube videos, and trawling through the Userguide, Unity Answers, Stack Overflow, and results from three search engines. I still can’t find anything that solves all this problem. It’s always just bits of it?

Ideally I need an extension method for Vector2/3 that takes: (Vector3 position, Quaternion rotation, Vector3 target) and returns the angle as +/- 0-180 degrees.

It needs to not be fooled by: The point being world right, but locally left of the character. The character and target being in negative world space, or the character / target crossing the 0,0 world origin.

Any help would be greatly appreciated, I’ve lost entire days to this now ☹

**Answer** by JVene
·
Oct 20, 2018 at 07:50 PM

Start with character.transform.forward. This gives you a vector from the character showing which way the character is facing (as if the character where at the origin). Use may require you know the character is not looking straight up, and I assume based on your diagram you need an xz plane (y axis) result.

Next, calculate a vector to the target - simply something like Vector3 totarget = target.transform.position - character.transform.position. Now you have two vectors, both from the viewpoint of the character's local position (as if it were at the origin).

Now, use Vector3 (or Vector2) SignedAngle method to calculate the angle between these two vectors. Note that in the standard of math (and of the libraries in Mathf), orientation of absolute angles is not "like the clock", as your diagram suggests. The documentation states SignedAngle gives the clockwise orientation between the vectors in the range of -180 to 180 degrees, but the standards of math work in Radians, and the orientation is quite awkward for students, at first. I believe SignedAngle may return what you expect, but if you further use the libraries for other calculations in trigonometry, you need to recognize not only do they use Radians, but zero degrees is X positive (3 o'clock), and positive rotations are counterclockwise (noon is -90 degrees).

Finally, you may need to use Vector2 to limit the result to the xz plane (y axis), as the Vector3.SignedAngle will give results of the angle in 3D, and won't translate into the xz plane (y axis), if that's what you require.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

making a camera that moves depending on mousepos.y 0 Answers

Rotate angle between two points 0 Answers

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer

Replace Quaternion with Vector2 in Quaternion times Vector3 equation? 1 Answer

Angle between two lines in 2D 3 Answers