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 Napoleonite · Jun 25, 2015 at 04:18 PM · angledegreescompassmeasure

[2D] Get 0-359 compass angle player <--> object

The closest I could find on the answerhub was:

     float AngleBetweenMouseAndPlayer()
     {
         Vector3 v3Pos;
         float fAngle;
 
         //Convert the player to Screen coordinates
         v3Pos = Camera.main.WorldToScreenPoint(transform.position);
         v3Pos = Input.mousePosition - v3Pos;
         fAngle = Mathf.Atan2(v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
         if (fAngle < 0.0f) fAngle += 360.0f;
         return fAngle - 90f;
     }

I can rework this code to work for 2 object <--> player. However, in this code the north returns 0, the east -90, the south 180 (the only correct one), and the west 90 degrees...

 I need a function that returns me (for 2D space):
 0 if the other object is exactly north of the player.
 90 if the other object is exactly east of the player.
 180 if the other object is exactly south of the player.
 270 if the other object is exactly west of the player.
 And of course all values in between, but no negative values.
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 Hellium · Jun 25, 2015 at 04:24 PM

The following method will give you the signed angle between your vectors (between -180° and 180°)

Simply add 180° to the result

     float SignedAngleBetween( Vector3 a, Vector3 b, Vector3 n )
     {
         float angle = Vector3.Angle( a, b );
         float sign = Mathf.Sign( Vector3.Dot( n, Vector3.Cross( a, b ) ) );
         return angle * sign;
     }

The n vector is the normal vector to your surface facing up (don't know the value for 2D games, Vector3.up I guess)

See Understanding Vector Arithmetic if you want to understand the mathematic notions involved :

http://docs.unity3d.com/Manual/UnderstandingVectorArithmetic.html

Comment
Add comment · Show 6 · 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 Napoleonite · Jun 25, 2015 at 05:12 PM 0
Share

The game-surface is along the XY-axis (topdown view, so the camera is aligned with the Z-axis) if that matters. The Unity default for 2D.

It doesn't work for me. I tried all 5 possible vectors for the normal including the zero. I add +180f to the result but still I get only values between ~180 and ~200. I get no values that make any sense to me. I also tried switching the a&b input parameters but that didn't help either.

I also tried changing Vector3.Angle( a, b ); to Vector2.Angle( a, b ) but it produced the same results (as expected).

avatar image Hellium · Jun 25, 2015 at 05:34 PM 0
Share

The angle is between direction, not points (it's impossible to have an angle between points). If you give position, it's equivalent to the direction (position - (0,0,0)) which gives you a direction.

alt text

avatar image Napoleonite · Jun 25, 2015 at 05:44 PM 0
Share

I think you are right. Perhaps my English isn't so good, my apologies. I changed the title. I guess I need the compass bearing, not the angle. Like if I had a compass and I needed it to point to a specific object (2D space) from the player, how do I find the rotation of the compass in degrees 0-359. I can't find a single topic about this.

avatar image Hellium · Jun 25, 2015 at 05:52 PM 0
Share

Then, keep the function I gave you (just change the vector3 to vector2), but as parameter, pass the following :

  // -Vector3.forward or Vector3.forward, don't really know
  float angle = SignedAngleBetween( Vector2.up , object.transform.position - player.transform.position, -Vector3.forward )
avatar image Napoleonite · Jun 25, 2015 at 07:09 PM 0
Share

Thanks a lot, that solved it. I had to add +360 because it went from 0 to $$anonymous$$us 180. Full unrefactored code(attached to the player gameobject):

  float SignedAngleBetween(Vector2 a, Vector2 b, Vector3 n)
 {
     float angle = Vector2.Angle(a, b);
     float sign = $$anonymous$$athf.Sign(Vector3.Dot(n, Vector3.Cross(a, b)));
     return angle * sign;
 }
 float AngleTo$$anonymous$$arker()
 {
     float angle = SignedAngleBetween(Vector2.up, $$anonymous$$arker.transform.position - transform.position, -Vector3.forward);
     if (angle < 0) { angle += 360f; }
     return angle;
 }
Show more comments

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

22 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

Related Questions

compass GUI jumps at angle 180. 0 Answers

Rotating A Character 180 Degress 1 Answer

By-pass the 'shortest route' aspect of Quaternion.Slerp 1 Answer

0-360 Y degree from Vector3.Angle 1 Answer

transform.lookat axis switch during rotation 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