- Home /
Trouble Rotating Full-Circle
Okay, so this seems like a really ridiculous question, but for some reason, I'm having problems. I'm just trying to make an object perform a full rotation along a single axis. It's part of a basic interpolation script to handle linear animations. Basically, you input a start position/rotation, an end position/rotation, and an animation time and the script determines how to move between them. With this particular example, I'm trying to rotate the object from 360 degrees to 0 degrees in the X axis (the script only uses positive numbers for rotation values).
My object rotates fine from 360 to 270 degrees, at which point, things start to go wrong. If using transform.Rotate, when the X rotation hits 270, the Y and Z axes jump to -180.
if ((openValue > closeValue && compareLeft < compareRight) || (openValue < closeValue && compareLeft > compareRight))
{
...
else if (transType == TransformType.ROTATE)
{
gameObject.transform.Rotate(openSpeed, Space.Self);
While the rotation itself is still correct, it throws off the calculation for determining the rest of the rotation - when the object reaches its halfway point, it now reads as having performed a full rotation on the X axis because its rotation is now (0, -180, -180).
I originally tried to counter this by resetting axes in which no rotation is occurring to their start values, but that just causes the object to hit 270 degrees and then begin stuttering, not moving any farther.
if (refAxis == AxisType.X)
{
if (closedCoords.y == openedCoords.y && gameObject.transform.localRotation.eulerAngles.y != openedCoords.y) { gameObject.transform.rotation = Quaternion.Euler(gameObject.transform.localRotation.eulerAngles.x, openedCoords.y, gameObject.transform.localRotation.eulerAngles.z); }
if (closedCoords.z == openedCoords.z && gameObject.transform.localRotation.eulerAngles.z != openedCoords.z) { gameObject.transform.rotation = Quaternion.Euler(gameObject.transform.localRotation.eulerAngles.x, gameObject.transform.localRotation.eulerAngles.y, openedCoords.z); }
}
So then, I tried to set the rotation value manually:
gameObject.transform.rotation = Quaternion.Euler((gameObject.transform.localRotation.eulerAngles.x + openSpeed.x), (gameObject.transform.localRotation.eulerAngles.y + openSpeed.y), (gameObject.transform.localRotation.eulerAngles.z + openSpeed.z));
Using this example, however, the object rotates to 270 degrees and then stops. I've checked the target value I'm trying to assign, and it appears correct, but the script will not set the object to that orientation. If I soft-code those values in the Inspector, it's not a problem, but somewhere under the hood, Unity doesn't like what I'm trying to do.
How can I get around this?
Any time you read eulerAngles, there is the potential for pain. Unity internally used Quaternions, and eulerAngles is derived from the Quaternion. There are multiple eulerAngles for any given physical rotation. For example setting (180,0,0) will come back as (0,180,180).
One trick is to treat eulerAngles as write-only. You keep your own Vector3 representing the rotation, and you make all of your modifications to that Vector3. You apply the Vector3 after you've made your modifications. That way you are always in control of the representation of the rotation.
Your answer
Follow this Question
Related Questions
How do you manually override mecanim's interpolation with rotations? 2 Answers
Quaternion.euler gives me glitch. Why? 1 Answer
Gun not rotating with the camera? 1 Answer
Weird rotation behaviour 1 Answer
Quaternion.Slerp problem... 1 Answer