0
\$\begingroup\$

In my game I'm trying to rotate a turret. I'm using the classic pattern:

float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");

if (h != 0 || v!=0)
    rb.transform.Rotate (new Vector3(v, 0, h));

The problem is that if I move in vertical axis also Y axis rotates. Why?

How can I achieve a rotation that rotates only the X and Z axis and not the Y axis?

\$\endgroup\$
0
\$\begingroup\$

The rotation is applied first around the z-axis, then around the x-axis and last around the y-axis. This means that rotating around the x-axis results in a rotation around the original y-axis aswell.

The easiest way to achieve what you want is probably to parent the object to another game object, and rotate the parent and child around the two axes separately.

That way the order of rotation doesn't matter. You need to set the localTransform of the child instead of the transform to get the correct behavior.

You might also want to keep the two angles in variables, and set the rotation using localTransform.rotation = Quaternion.Euler(x,y,z), instead of applying the rotation to the current transform, because that will be more robust against rounding errors and the state is easier to inspect and control.

Example (untested):

baseRotation += Input.GetAxis ("Horizontal");
barrelRotation += Input.GetAxis ("Vertical");
turretBase.localTransform.rotation = Quaternion.Euler(0, 0, baseRotation);
turretBarrel.localTransform.rotation = Quaternion.Euler(barrelRotation, 0, 0);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.