In Unity the angle at 0 degrees is pointing up and to rotate this angle 360 degrees we rotate it clock wise.
This is quite different from the unit circle used in mathematics. The unit circle has 0 degrees pointing right and to rotate it 360 degrees we rotate it counter clock wise.
Ultimately what I want to do is to create a unit vector from the rotation of a GameObject
’s forward direction.
After an hour of scratch work and testing I came up with a solution that I think sovles my problem.
// new algo
angle += 90;
angle -= angle * 2 + 180;
// convert degrees to radians
angle *= Mathf.PI / 180;
float deltaX = Mathf.Cos (angle) * magnitude;
float deltaZ = Mathf.Sin (angle) * magnitude;
vector3 = new Vector3 (deltaX, 0f, deltaZ);
Note that the angle
is gameObject.transform.rotation.eulerAngles.y
in my code.
Ran into the exact same issue and I did some of my own scratch work to try and optimize the above:
float radians = angle * Mathf.Deg2Rad;
float x = Mathf.Sin(radians);
float z = Mathf.Cos(radians);
return new(x, 0f, z);