Search code examples
c#unity-game-enginemath-functions

Why is the Cos function in C# giving me wrong answers?


I have a method, in which I am using the Cos() Function from the namespace "Mathf". Sadly, when I give, for example the in put of 45 it returns 0,52532, which is wrong. Using a calculator I get the correct answer of 6.283185 Radians.

float angle = 45;
Debug.Log(Mathf.Cos(angle));

Prints out:

0,525322

Can anyone help me out here? I am very confused right now


Solution

  • You need to convert to radians.

    float angle = 45;
    Debug.Log(Mathf.Cos(angle * Math.PI / 180.0f));