• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lemmex · Oct 20, 2018 at 05:19 PM · vector3quaternionanglevector2

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 ☹ alt text

findanglewithdirectionoftravel-01.png (95.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Angles from quaternion/vector Problem. 2 Answers

how to convert the angle to roatation? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges