Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by calebaker · Aug 20, 2015 at 07:38 PM · functionmathcircledegreescalculate

How to get 0-360 degree from two points

Is there a function that will return degrees (0-360) from one point (x = 0, z = 0) to another point (-x = 3, -z = 5)

alt text

Edited to add exact code snippet that worked for me.

 GameObject target = GameObject.Find("Name Of Game Object In Hierarchy");

 float MyPositionX = transform.position.x;
 float MyPositionZ = transform.position.z;
 float TargetPositionX = target.transform.position.x;
 float TargetPositionZ = target.transform.position.z;

 degree = FindDegree(MyPositionX - TargetPositionX, MyPositionZ - TargetPositionZ);   

  public static float FindDegree(float x, float y)
  {
      float value = (float)((System.Math.Atan2(x, y) / System.Math.PI) * 180f);
      if (value < 0) value += 360f;
      return value;
  }
how-to-get-degrees.png (10.7 kB)
Comment
Add comment · Show 2
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 meat5000 ♦ · Aug 20, 2015 at 04:17 PM 0
Share

You can use Vector3.Angle and make it relative to some given world axis.

avatar image VesuvianPrime · Aug 20, 2015 at 04:43 PM 1
Share

You'll also need to correct for the case where the angle is >180, since Vector3.Angle returns the acute angle.

2 Replies

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

Answer by YoungDeveloper · Aug 20, 2015 at 07:42 PM

This will take classic x and y

 private void Start(){
     Debug.Log(FindDegree(0, 1));
 }
 
 public static float FindDegree(int x, int y){
     float value = (float)((Mathf.Atan2(x, y) / Math.PI) * 180f);
     if(value < 0) value += 360f;
 
     return value;
 }

If you want to use this from transform positions, then vector3.angle is what you are looking for. As you have to know what is forward to calculate it, i believe there even a ready example if you google.

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 Bunny83 · Aug 20, 2015 at 10:10 PM 0
Share

btw: $$anonymous$$athf does also have a PI constant as float, so that casting from double to float wouldn't be necessary. There are also two conversion constants:

  • $$anonymous$$athf.Deg2Rad

  • $$anonymous$$athf.Rad2Deg

Those are factors so you just have to multiply an angle in radians with "$$anonymous$$athf.Rad2Deg" to get degrees.

Also keep in $$anonymous$$d that the parameters of the Atan2 method are swapped. The parameters are y and x. The example on the docs page is actually wrong ^^. They are swapped because it actually calculates the arctangent of "y / x" and in addition takes the quadrant into account by looking at the signs of x and y. It also handles the case of "x == 0" to prevent a division by 0. See wikipedia as well.

avatar image YoungDeveloper · Aug 21, 2015 at 10:33 AM 1
Share

@Bunny83 I actually use my own $$anonymous$$ath lib, where i have my own constant PI. It was actually a typo from my side using $$anonymous$$athf.Atan2 i meant $$anonymous$$ath in this example. As i share my libs in various c# projects i never use unity $$anonymous$$athf lib, as its pretty much a wrapper around System.$$anonymous$$ath. For example here's a source for $$anonymous$$athf.Atan2().

 public static float Atan2(float y, float x){
     return (float)$$anonymous$$ath.Atan2((double)y, (double)x);
 }

So we have ton of casts here, what i do in my libs is usually calculate evetyhing and only then cast once. But even that down cast is zero overhead, as its pretty much a number cut down, not even a round.

What comes to the function i posted, i know about atan swap, but if I swap Atan2 correctly , just like docs say, it actually produces mirrored results for x axis. I did tests with these examples, and results are perfect. ;)

 Debug.Log(FindDegree(0, 0));   //0 - 360
 Debug.Log(FindDegree(0, 1));   //0 - 360
 Debug.Log(FindDegree(1, 1));   //45
 Debug.Log(FindDegree(1, 0));   //90
 Debug.Log(FindDegree(0, -1));  //180
 Debug.Log(FindDegree(-1, -1)); //225
 Debug.Log(FindDegree(-1, 0));  //270
 Debug.Log(FindDegree(-1, 1));  //315

avatar image Bunny83 · Aug 21, 2015 at 11:20 AM 0
Share

@YoungDeveloper: I know that Unity's $$anonymous$$athf class is just a wrapper for System's $$anonymous$$ath class. I didn't ment the cast overhead but the cast just makes it harder to read ^^. That's actually the reason why Unity provided that wrapper class for $$anonymous$$ath since Untiy only works with floats ins$$anonymous$$d of doubles.

If you use your own $$anonymous$$ath lib (or System's one) you should do:

 public static float FindDegree(double x, double y){
     double value = ($$anonymous$$ath.Atan2(x, y) / $$anonymous$$ath.PI) * 180;
     if(value < 0d) value += 360d;
     return (float)value;
 }

to $$anonymous$$imize casting and precision problems. Since your x and y are casted to double anyways you can simply define your parameters as double. ints and floats are implicitly casted to double.

$$anonymous$$irrored results? In mathematics the unit circle has 0° on the right and goes counter clockwise around the circle.

  x, y
  -----------
  1, 0 -> 0°
  1, 1 -> 45°
  0, 1 -> 90°
 -1, 1 -> 135°
 -1, 0 -> 180° // or -180°
 -1,-1 -> 225° // or -135°
  0,-1 -> 270° // or -90°
  1,-1 -> 315° // or -45°

The given angle should result in this vector:

 Vector2(Cos(angle), Sin(angle))

Which is the "opposite" operation of Atan2.

$$anonymous$$eep in $$anonymous$$d this is a pure mathematics function and definition. In the context of Unity everything is a bit different since in mathematics you usually work with a right-handed system while Unity uses a left-handed system. Likewise as you might know there are many different diffinitions for Euler angles and Unity only uses one of them ^^.

avatar image YoungDeveloper · Aug 21, 2015 at 11:35 AM 1
Share

@Bunny83 yeah thats exactly what i do in my libs, i tried to write it fast unity style for the op lol.

avatar image Tovey-Ansell · Nov 15, 2016 at 03:21 PM 0
Share

Sorry to dig up an old thread, but this just wont work for me...

I have the exact same FindDegree method, but when giving it the values 0 and 25, it returns 0 as the angle? Has something changes since to make this method not work any more?

avatar image calebaker Tovey-Ansell · Nov 15, 2016 at 09:05 PM 0
Share

That's actually correct, if you drew a line from 0 strait up 25 units, the degree to that point from 0 would be 0 degrees or 360 degrees.

avatar image
6

Answer by eppz · Feb 15, 2017 at 12:39 PM

I made an extension, use like this:

 float angle = transform.position.AngleTo(touch.position);

Just put this into a CS file somewhere:

 public static class _Vector2
 {
     public static float AngleTo(this Vector2 this_, Vector2 to)
     {
         Vector2 direction = to - this_;
         float angle = Mathf.Atan2(direction.y,  direction.x) * Mathf.Rad2Deg;
         if (angle < 0f) angle += 360f;
         return angle;
     }
 }

This is not (!) mirrored, as Atan2 takes y coordinate first.

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 PabloCorso · Oct 03, 2017 at 01:19 AM 1
Share

This is great.

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

9 People are following this question.

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

Related Questions

Find position of pixel on sphere given position of pixel on texture 1 Answer

Calculating triangle side length based on hypotenuse? 1 Answer

Detect GUI rotation degree 1 Answer

How to make a wavy circle? 0 Answers

Launching object in circular motion 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