- Home /
Quaternion and 360 degree rotations
Hi guys, I'm using the following co-routine to rotate a gameobject over an amount of time:
IEnumerator RotateObject(float time, Quaternion targetRotation)
{
float tParam = 0.0f;
Quaternion currentRotation = transform.rotation;
// Compares angles of two Quaternions
while (Quaternion.Angle ( transform.rotation, targetRotation) >0.1f) {
tParam += Time.deltaTime * (1 / time);
transform.rotation = Quaternion.Slerp (currentRotation, targetRotation, tParam);
yield return null;
}
// Hard lock the rotation
transform.rotation = targetRotation;
}
However, I need a function which rotates an object around 360 degrees - with the current code it'll just take the shortest route (so if I specify 360 degrees it won't move). So I want to be able to effectively force a direction of rotation. Any ideas how to best accomplish this?
Thanks in advance
Answer by zombience · Nov 05, 2013 at 08:46 PM
try using your transform's eulerAngles, and updating the rotation that way. It would be similar to using transform.Rotate() except hopefully this way you could have a little easier time counting how long it has been rotating.
Vector3 rot = transform.rotation.eulerAngles;
rot.y += 1f;
transform.rotation = Quaternion.Euler(rot);
Putting something like that in your Coroutine could be what you're looking for. either that or transform.Rotate could work, you'd just have to keep track manually of how far you've rotated.
Changing euler angles may be the way to go, but not using them directly (see the docs warning about this.) They will snap values at odd times (you can set x to 180, and get x=0 and y and z have changed.)
Ins$$anonymous$$d, keep your own global Vector3 for x,y,z rotation. Change that and set rotation using it.