Need help with rotation

Imagine a you are looking at a cube, on the front of it. Let’s call the axis that goes from side-to-side of the cube for X, top-to-bottom Y and front-to-back Z. What I want is for movement to the right/left should always rotate the cube right/left, while moving up/down always rotate up/down.

If we look at the cube from the front and walk down/up on it would rotate around the X-axis.

If we look at the cube from the front and walk left/right it would rotate around the Y-axis.

As long as we continue in either those directions it would continue to rotate around the same axis.

Now, imagine that the axes can’t Gimbal lock. That is, when you rotate around either axes both other axes would rotate an even amount.

So, back to the front of the cube. We walk to the right and the cube rotates around the Y axis. We are now looking at the “right side” of the cube (as it was originally positioned). If we now move up, the cube should rotate around the Z-axis.

And so on.

I’ve been trying to figure out how to this for quite a few hours now, but I just can’t get my head around it. It would be quite easy to do if the axes rotated as I “imagined” above, since I then could always rotate +/- 90 degrees around the correct axis, but that’s not really how it works, is it?

I think you want transfrom.Rotate() using the Space.World option.

–Eric

Rotations are nasty things. I have a camera that rotates with your mouse. It wrks fine as long as the original rotation has angles of 0,90,180 or 270. But any other rotation on either axis will cause it to behave different.

What I did was add the camera to an empty parent. The parent rotates up and down and the camera child rotates only left and right.

This is the script that belongs to it. Maybe it is of any help.

``````function Update () {
if(Input.GetMouseButton(0) || Input.GetMouseButton(1)) {  // right or left mouse button pressed
var rotationX = Input.GetAxis("Mouse X") * mouseSensitivityX;
var rotationY = Input.GetAxis("Mouse Y") * mouseSensitivityY;
rotationX = ClampAngle (rotationX, mouseMinimumX, mouseMaximumX);
rotationY = ClampAngle (rotationY, mouseMinimumY, mouseMaximumY);
transform.parent.rotation *= Quaternion.AngleAxis (rotationX, Vector3.up);
transform.rotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
}
}
``````

Hi Eric,

That was exactly what I was looking for. After reconsidering how to get it implemented things are now working super fine. Apart from one thing. I want the rotation to happen over time.

Any easy suggestions? I can create a dummy GameObject to hold the final rotation, then simply do a Lerp from that dummies eulerAngles and my real objects eulerAngles.

Or I could create a counter and a while loop, then do a manual Time.deltaTime kind of deal. The problem with that solution is that I have to manually snap the rotation after it’s done to make sure it’s aligned perfect to the axes.

Hi Mocking,

That solution seems familiar. I recall someone used something similar in Ogre when I was looking at camera movement. Thanks for the tip.