Is it possible to rotate an object around its z-axis without changing its x-axis and y-axis?
Hi Unity Answers,
I'm currently working on a small game involving a rotating cube. The player clicks a button, which Lerps the cube's rotation on the z-axis by 90 degrees. This works fine, but if I click the other button (which rotates the cube around the x-axis), the cube will behave strangely. Sometimes it will rotate in the wrong direction, sometimes it will jitter. But I know that one of the key problems here is the z-axis rotating the x-axis during the Lerp sequence.
Here is the code I use to lerp:
IEnumerator LerpRotation(float a_Start, float a_End, float a_Duration, string a_Axis)
{
float i = 0;
float Rotation;
//A stylistic choice meant to make the cube rotate faster until it finishes.
float IncreaseRate = 0.0002f;
while (i < 1)
{
//a_Duration, in this scenario, is 2.0f
i += Time.deltaTime * a_Duration * IncreaseRate;
Rotation = Mathf.LerpAngle(a_Start, a_End, i);
switch (a_Axis)
{
case "x":
WonderCube.transform.eulerAngles = new Vector3(
Rotation,
WonderCube.transform.eulerAngles.y,
WonderCube.transform.eulerAngles.z);
break;
case "y":
WonderCube.transform.eulerAngles = new Vector3(
WonderCube.transform.eulerAngles.x,
Rotation,
WonderCube.transform.eulerAngles.z);
break;
case "z":
WonderCube.transform.eulerAngles = new Vector3(
WonderCube.transform.eulerAngles.x,
WonderCube.transform.eulerAngles.y,
Rotation);
break;
}
if (IncreaseRate < 2.0f)
IncreaseRate += 0.05f;
yield return null;
}
}
Again, this seems to work fine for the z-axis, but as soon as the x-axis is involved, the cube starts misbehaving. I've done some research and found something called Gimbal Lock (found here: http://answers.unity3d.com/questions/573035/avoiding-gimbal-lock.html), which might be related. However, I'm unsure if this is the same thing. I'd just like to be able to rotate around the z-axis without adjusting the direction of my x-axis and y-axis.
I'm thinking a dummy that doesn't rotate might work, but RotateAround is (according to Unity) obsolete, and I'm not sure how to use a dummy's axis for rotation.
I'd really appreciate some help on what I can do to improve this rotation code. Thanks very much.
EDIT: I've continued researching the problem, but I haven't found any solutions. I've tried researching if I was rotating in local space, but i'm using eulerAngles which is in World Space.
I also recorded a gif of the issue. Sorry for the low quality: http://imgur.com/a/BbwS7
It's hard to see it, but the jitter I mentioned is demonstrated in that gif. When I click the right button twice, the Y rotation and the Z Rotation jump to 180, despite the fact that only the X Rotation should be changed here.
I'd also like to clarify, as I have a better grasp on the problem now: my issues are that the rotation of the cube is follow the body, which I don't want, and that the cube is acting strangely when I rotate it on the X-Axis (regardless of the orientation of the Z-Axis and Y-Axis, as seen in the gif).
Angles snapping by 180 is normal - its because you're not supposed to modify single eulerAngles that way. x is also local, not global. EulerAngles are applied y,x,z, so only y is global.
You can probably use transform.Rotate(xx, Space.World). But for something more versatile, Quaternions and their math is better. I've got something at taxesforcatses (in the move/rotate) section, or you can probably find quaternions for beginners somewhere.
Answer by dpoly · Dec 12, 2016 at 07:54 AM
The basic answer to any question involving gimbal lock is: use quaternions not Euler angles. They work for cameras, rotating/tumbling objects, whatever. Recommended.
In most cases the problem just goes away. Quaternion.RotateTowards()
or QuaternionFromToRotation()
is your friend.
Your answer
Follow this Question
Related Questions
Rotate on Transform.localeulerAngels smoothly 0 Answers
How to smoothly rotate gameobject 20 degrees on key press? 0 Answers
fromtorotation inacurate Help fixing? 0 Answers
Rotation (Quaternion.Lerp) not working 0 Answers
Smooth Rotation Quaternion Lerp? 1 Answer