I'm trying to convert some rotational values in C# for Unity. I want to be able to let a user enter a value between 0 and 360 degrees and this value needs to be mapped to two axes, x and z. Both the x and z axes go from -1 to 1 and I'm wondering how to convert the degrees into the desired range. This is even more confusing since only both axes together determine the rotation.
The two axes come from Camera.forward.x
and Camera.forward.z
. I could of course just have values between -1 and 1 entered for both axes but it would be more natural to have a degree between 0 and 360 entered. How would I achieve this?
UPDATE:
My code now looks like this to convert the degrees into radians for x an z:
// rotation is a value between 0.0 and 360.0
float rad = rotation * Mathf.Deg2Rad;
float z = Mathf.Sin(rad);
float x = Mathf.Cos(rad);
Debug.Log(x + " " + z);
However depending on the rotation value, the result isn't always between -1 and 1. For example:
- If I set rotation to 0 I get
x:1 z:0
- If I set it to 360 I get
x:1 z:1.748456E-07
- If I set it to 90 I get
x:-4.371139E-08 z:0
What's going on there?
UPDATED LOG:
rotation:0 rad:0 z:0 x:1
rotation:90 rad:1.570796 z:1 x:-4.371139E-08
rotation:180 rad:3.141593 z:-8.742278E-08 x:-1
rotation:360 rad:6.283185 z:1.748456E-07 x:1