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 Kreibich · Nov 22, 2013 at 12:28 AM · anglevector

Vector to angle

Hi, I would like to know how to convert Vector to Angle.

For example:

x : y -> Expected result


0 : 1 -> 0

1 : 0 -> 90

  • : 0 -> 270

0 : -1 -> 180

1 : 1 -> 45

  • : 1 -> 315

1 : -1 -> 135

  • : -1 -> 225

Is there any way to count it?

Thanks.

Comment
Add comment · Show 4
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 Huacanacha · Nov 22, 2013 at 01:09 AM 0
Share

Have you looked at Vector2.Angle? According to the Vector3 docs that will give you the acute angle (not greater than 180 degrees), so if that doesn't work for you take a look at Quaternions. I think something like this should work but I can't test it right now:

 Quaternion.LookRotation(yourVector2).eulerAngles.z

That will give you the rotation around the Z axis, which is equivalent to what you want.

avatar image Kreibich · Nov 22, 2013 at 01:21 AM 0
Share

I have tried:

float angle = Vector3.Angle(new Vector3(0.0f, 1.0f, 0.0f), new Vector3(x, y, 0.0f)); ... but it returns an absolute value of an angle (for example vector -1 : 1 returns 45 ins$$anonymous$$d of 315)

avatar image Kreibich · Nov 22, 2013 at 01:22 AM 0
Share

Thanks, but I need an angle greater than 180.

avatar image Huacanacha · Nov 22, 2013 at 01:26 AM 0
Share

Did you try the Quaternion approach? You'll may need to +/- 90, 180, or 270 degrees to the result (and if so adjust to get the 0-360 range) but it should work.

3 Replies

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

Answer by Kreibich · Nov 22, 2013 at 01:31 AM

Solved!

          float angle = Vector3.Angle(new Vector3(0.0f, 1.0f, 0.0f), new Vector3(x, y, 0.0f));
         if (x < 0.0f) {
             angle = -angle;
             angle = angle + 360;
         }
Comment
Add comment · Show 3 · 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 · Nov 22, 2013 at 01:42 AM 0
Share

You could combine the two lines in the if

     angle = 360f - angle;
avatar image Kreibich · Nov 22, 2013 at 01:57 AM 0
Share

Oh sure, thanks... it's 2:56 a.m. here and my brain is almost in hybernation state :)

 float angle = Vector3.Angle(new Vector3(0.0f, 1.0f, 0.0f), new Vector3(x, y, 0.0f));
 if (x < 0.0f) angle = 360.0f - angle;
avatar image Bunny83 · Nov 22, 2013 at 02:57 AM 0
Share

Well, i have the same time here ;) (Now 3:56 :D)

avatar image
5

Answer by Bunny83 · Nov 22, 2013 at 01:39 AM

Well, you have two unusual things (compared to plain Trigonometry). o° is normally right (1,0) and it usually goes counter clockwise, so 90° would be up (0,1).

To get the angle you usually use atan2 (in Unity Mathf.Atan2).

However if you swap x and y it should be what you want since swapping x<--->Y should mirror everything on the main diagonal.

Something like this should work:

     var angle = Mathf.Atan2(V.x, V.y) * Mathf.Rad2Deg;


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 Simpowitch · Sep 29, 2020 at 04:39 PM 0
Share

Thank you. I have been searching for 2 hours and everyone just says: "OBSERVE THAT YOU NEED TO USE "Atan2(y, x)", and I could not for the world get it to work. Thanks for explaining both parts

avatar image
3

Answer by aldonaletto · Nov 22, 2013 at 02:12 AM

Since it's 2D, a little trig can do the magic:

 function Angle(v: Vector3): float {
   // normalize the vector: this makes the x and y components numerically
   // equal to the sine and cosine of the angle:
   v.Normalize();
   // get the basic angle:
   var ang = Mathf.Asin(v.x) * Mathf.Rad2Deg;
   // fix the angle for 2nd and 3rd quadrants:
   if (v.y < 0){
     ang = 180 - ang;
   } 
   else // fix the angle for 4th quadrant:
   if (v.x < 0){
     ang = 360 + ang;
   }
   return ang;
 }

For C#, just change the first line as below (the rest of the code is the same):

 float Angle(Vector3 v){
   ...

  
  
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 BeregAlto · Apr 04, 2020 at 06:12 AM 0
Share

For C# and unity I tranformed it a bit:

     private float Angle(Vector3 v){
         // normalize the vector: this makes the x and y components numerically
         // equal to the sine and cosine of the angle:
         v.Normalize();
         // get the basic angle:
         var ang = $$anonymous$$athf.Asin(v.y) * $$anonymous$$athf.Rad2Deg;
         // fix the angle for 2nd and 3rd quadrants:
         if (v.x < 0){
             ang = 180 - ang;
         } 
         else // fix the angle for 4th quadrant:
         if (v.y < 0){
             ang = 360 + ang;
         }
         return ang;
     }

And use it like:

 playerOrientation.rotation = Quaternion.Euler(0,0, Angle(rb.velocity.normalized));

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

21 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

Related Questions

Orienting multiple objects on the same line 1 Answer

Finding rotation 0 Answers

[2D] Angle to Vector conversion 1 Answer

Make an object move to a given point by rotating to the correct direction first. 1 Answer

Get new Point from Vector and Angle 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