The trouble with Angles

The problem is this:

Both calculations give you the same result!

float a = Vector2.Angle(Vector2.up, new Vector2(5f, 2f)); //a = 68.19859
float b = Vector2.Angle(Vector2.up, new Vector2(-5f, 2f)); //b = 68.19859

So how can you get the angle of a vector between 0 and 360 degrees?

double radians = Mathf.Atan2((y2-y1), (x2-x1));
double degrees = radians * 180f / Mathf.PI;
if(degrees < 0) degrees += 360; // degrees are now between 0 and 360

float angle = (float) degrees; //cast to float if you need to

Just use the correct orientation for (x1, y1) and you’re good…

    Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

    Post a Comment

    Your email is never published nor shared. Required fields are marked *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>