All I want is to turn a Vector2 into a radians angle. Why is this so hard?!

Let’s go back to highschool: if I want to take an x-value and a y-value on a unit circle and get the angle in radians, I use cotangent, right? Which can be done using tan(x/y), right? After spending hours trying to get this to work in Unity, I tried this test:

for (float i = -1; i < 1; i = i + 0.2f) {
            for (float j = -1; j < 1; j = j + 0.2f) {
                Vector2 test = new Vector2(i, j);
                test.Normalize();
                Debug.Log("For " + test.x + "," + test.y + ", the alleged cotangent is " + Mathf.Tan(test.x / test.y));
            }
}

This confirmed a lot of confusing outputs I was seeing, like “tan(0.5/0.58) = -10.4” I got similarly bizarre results from Mathf.Tan(test.y / test.x).

Evidently I’m smart enough to make it through Algebra 2 and Calculus classes, but I’m not smart enough for this. I do understand that radians can be any number, but I expect to see outputs between negative-pi and pi. Or zero and 2 * pi. Can anyone explain to me what’s causing this? Or at least point me to a suitably condescending tutorial?

Maybe you want Atan2? (the Unity Mathf.Atan2 version returns radians)

https://stackoverflow.com/questions/15994194/how-to-convert-x-y-coordinates-to-an-angle

1 Like

You are confusing tangent, cotangent, and arctangent. If you have a vector and you want an angle, you want the arctangent (i.e. the inverse of the tangent function), which you can get with the function Math.Atan2. (Or Math.Atan, but Atan2 is usually preferable.)

1 Like

@PraetorBlue @Antistone

That did it, guys. Thank you.

1 Like