- Home /
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.
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.
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)
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.
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; }
You could combine the two lines in the if
angle = 360f - angle;
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;
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;
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
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){
...
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
![](https://koobas.hobune.stream/wayback/20220613122442im_/https://answers.unity.com/themes/thub/images/avi.jpg)