Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
This question was closed May 14, 2013 at 11:40 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by TheDarkVoid · Apr 28, 2013 at 09:33 PM · anglevector2

Calculating the Angle of a Vector2 from zero

Here is an example if what I want.

alt text I've tried this code:

             float vectorAngle = 0; 
             if(InputC.TorqueVector != Vector2.zero)
                 vectorAngle = Vector2.Angle(InputC.TorqueVector - vectorZero, vectorZero)-90;
             vectorAngle = 360-vectorAngle;

where vectorZero is (0,1) and TorqueVector is the vector i'm trying to measure the angle of. with this i'm able to get most vectors to work except vectors with -y components. what am i doing wrong?

Edit: My New code is:

             float vectorAngle = Mathf.Acos(Mathf.Deg2Rad*InputC.TorqueVector.x);
             vectorAngle *= Mathf.Rad2Deg;
             if(InputC.TorqueVector.y < 0)
                 vectorAngle*= -1;

This works correctly for vectors (-20,0) & (20,0) but for vectors (0,0),(0,20),(0,-20) i get angles near 90. what am i missing?

untitled-1.png (46.1 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 Andrii · Dec 24, 2016 at 04:30 PM 0
Share

Based on responses in this and other threads, these are my two functions to convert Vector2 to degrees and back. Used them in my pong clone, so they are tested and working.

 //Convert Vector2 to angle in degrees (0-360)
 public static float Vector2Angle(Vector2 v){
     if(v.x<0) return 360-($$anonymous$$athf.Atan2(v.x, v.y)*$$anonymous$$athf.Rad2Deg*-1);
     else return $$anonymous$$athf.Atan2(v.x,v.y)*$$anonymous$$athf.Rad2Deg;
 }
 
 //Convert degrees (0-360) to a Vector2
 public static Vector2 Angle2Vector(float angle,float power){
     return (new Vector2($$anonymous$$athf.Sin(angle*$$anonymous$$athf.Deg2Rad),$$anonymous$$athf.Cos(angle*$$anonymous$$athf.Deg2Rad)))*power;
 }

avatar image andrew_pearce · Apr 11, 2020 at 07:18 AM 0
Share

Just in case if someone will search for a solution and will find this topic via google, here is the working solution:

     public float GetAngleFromVector(Vector2 vector) {
       vector = vector.normalized;
       float angle = $$anonymous$$athf.Atan2(vector.y, vector.x) * $$anonymous$$athf.Rad2Deg;
       return (angle < 0) ? angle + 360 : angle;
     }

2 Replies

  • Sort: 
avatar image
5
Best Answer

Answer by DESTRUKTORR · Apr 28, 2013 at 09:44 PM

The angle of a 2D vector can be calculated a number of ways. Mathf.ACos(InputC.TorqueVector.x) or Mathf.ASin(InputC.TorqueVector.y) will get you the angle, relative to 0, but it will not retain the sign. In other words, if the actual angle is pi/4 or -pi/4, the answer you'll get back will be pi/4. To rectify this, simply check the sign of the y-coordinate. If it is positive, the angle will remain positive, if it is negative, multiply the angle by -1.

The same issue comes up with Vector2.Angle(). So the answer you're getting is correct, given what you've told it to do. You just need to account for the y-coordinate's sign.

NOTE: If you're going to use the trig functions in Mathf, if you're inputting an angle, CONVERT TO RADIANS, FIRST. Despite the Vector classes giving angles in degrees, and some rotation-functions operating in degrees, check the specific documentation for each class before assuming the input or returned values will be in either degrees or radians.

[EDIT] I noticed there were a few mistakes in my logic, here. First, the value you're looking for is an angle. The value you're plugging in is a coordinate. Only convert to radians if you're inputting an angle. Secondly, it is typically more accurate to use ATan than ACos or ASin, as it takes both coordinates into account. If you have a zero-vector, either do nothing (because technically there -is- no angle) or return 0.

Comment
Add comment · Show 4 · 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 TheDarkVoid · Apr 28, 2013 at 09:59 PM 0
Share

This makes a lot more sense i noticed that it was only giving me a 4th of what i need when i tried some stuff with Asin, i'm going to try this. Thanks

avatar image TheDarkVoid · Apr 28, 2013 at 10:14 PM 0
Share

This worked partially, i've updated the question with the new issue.

avatar image DESTRUKTORR · Apr 28, 2013 at 10:36 PM 0
Share

Updated the answer. I dun made some mistakes in my calculations, and there was at least one I hadn't touched on, with the program$$anonymous$$g of this (zero vector and zero coordinates).

avatar image TheDarkVoid · Apr 28, 2013 at 11:40 PM 1
Share

use Atan(y/x) works a lot better, thanks and to fix my issue i just added 180 to the angle when y is negative and multiply by -1 when x is negative

avatar image
10

Answer by sandolkakos · Oct 13, 2013 at 12:01 AM

Here is my solution:

 public static float Angle(Vector2 vector2)
 {
     if (vector2.x < 0)
     {
         return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * -1);
     }
     else
     {
         return Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg;
     }
 }


Edit: Adding a Mathf.Sign to return 1 or -1 and keep the math in just one line. Thanks @jardineiro

 public static float Angle(Vector2 vector2)
 {
     return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * Mathf.Sign(vector2.x));
 }

Comment
Add comment · Show 2 · 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 jardineiro · Feb 04 at 06:10 PM 1
Share

Thanks. This was very helpful. One note: You can simplify a bit with Mathf.Sign to return 1 or -1 based on the sign of the x value.

public static float Angle(Vector2 p_vector2) { return 360 - (Mathf.Atan2(p_vector2.x, p_vector2.y) Mathf.Rad2Deg Mathf.Sign(p_vector2.x)); } }

avatar image Shellander · Mar 25 at 09:02 PM 0
Share

The first solution works very well, but the simplified one (the second one) only returns 180+ values for me.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Convert a 360 bearing into a Vector 2 1 Answer

Why angle between these two velocities is 45? 2 Answers

Feild of view only wokring in specific areas of the scene 0 Answers

Get angle between a position and facing direction 0 Answers

Move a game object away from another game object based on the angle difference 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