Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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

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

Camera FOV lower than 1? 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