• 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 hilariousjello · Mar 14, 2014 at 03:11 AM · topdownsortingfieldofviewvector3.angle

Angle Between Points

I am making a top-down field-of-view system similar to Monaco. I've managed to assemble a list of all the corners of walls visible from the player's position (as Vector3s), and I need to sort them in a clockwise order around the player. I've already tried to implement a solution using Vector3.Angle and this, but it isn't working and returns largely nonsensical angles. How might I go about sorting these points?

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 robertbu · Mar 14, 2014 at 03:41 AM

I believe you only need the angles on a single plane (i.e. you are not looking for angles in 3D space. If so, you can use Mathf.Atan2(). I cannot tell from your description if you game is played on the XY plane or the XZ plane. I also cannot tell what side of your character is considered forward. So let's say that it is played on the XY plane and you consider the right side of the character as forward. To start with you can get a base angle from the forward of the character. Then for all the objects you can get a normalzied angle relative to the character.

 var dir = transform.right;
 var charAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

Then for each object you can do something like:

 dir = obj.position - transform.position;
 var objAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - charAngle;
 if (objAngle < 0.0) objAngle += 360.0;

The result will be an angle relative to the player that grows counterclockwise. For a clockwise rotation, simply sort the angles in descending order. Note for your purposes you don't need to convert from radians to degrees. You can leave off the Mathf.Rad2Deg and just add Mathf.PI * 2 for negative angles.

Comment
Add comment · Show 1 · 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
avatar image hilariousjello · Mar 14, 2014 at 05:28 PM 0
Share

Works great! Thank you!

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

20 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

Related Questions

Proper/Standard way to create a top down / isometric sprite sorting solution 1 Answer

CustomAxis sort not working between objects placed at different layers rendered by multiple cameras 0 Answers

2D: Transparency Sort Axis not properly affecting Tilemaps? 2 Answers

How to create a field of view for enemy ai that detects a player Unity 2d? 3 Answers

Unity 2D Collider did not create any collision shapes as they all failed verification 0 Answers


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