- Home /
Odd discrepancy with EulerAngles (Circular motion, C#)
I'm working on creating a circular motion while the object itself rotates to follow the circular path it is on. While the motion isn't much of a problem, the rotation seems to be slightly off and I can't figure out why.
I use the code below to move the object, as well as rotate it. While it seems to be -almost- fine, the rotation always seems to be off just a bit. I've traced a few things and found out the following: When I set the angle to zero, by my code the rotation of the object should be 360 degrees when I run the script. When I trace the yRotation variable it reflects this nicely. However, when I look at the actual rotation in the editor it gives me a rotation on the y-axis of 106.
I try not to use Rotate.LookAt, Transform.RotateAround or anything of the sort.
Does anyone know why this is?
void Update () {
angle += Input.GetAxis("Horizontal")*10*Time.deltaTime;
Vector3 newPosition = transform.position;
newPosition.x = Center.x + radius*Mathf.Cos(angle);
newPosition.z= Center.z + radius*Mathf.Sin(angle);
float yRotation = 360 - angle;
print(yRotation);
transform.rotation = Quaternion.EulerAngles(0,yRotation,0);
transform.position = newPosition;
}
Answer by aldonaletto · Feb 15, 2012 at 10:57 PM
You're mixing degrees and radians: Mathf.Sin, Mathf.Cos and Quaternion.EulerAngles are functions that receive arguments in radians, not degrees.
If you want to keep things in radians, define only the starting angle in degrees and convert it to radians:
public float startAngle = 0; // starting angle in degrees
void Update(){ ... float yRotation = startAngle * Mathf.Deg2Rad - angle; transform.rotation = Quaternion.EulerAngles(0,yRotation,0); transform.position = newPosition; } NOTE: Quaternion.EulerAngles(...) is a deprecated function that receives angles expressed in radians (it's not even listed in the docs). For angles in degrees, use Quaternion.Euler(...) instead.