- 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

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.

### Follow this Question

### Related Questions

Rotate vector around vector? 1 Answer

Random perpendicular vector3 after rotation 1 Answer

Rotate floor plane in-game via C# script 1 Answer

Angle between two lines in 2D 3 Answers

Raycasting at an offset angle? 3 Answers