Leon Ludwig
Leon Ludwig

Reputation: 11

Rotate an Object on X and Z GLOBAL axis

i have the following scenario:

I have a cylinder in my scene and I hace two Dropdowns. With the Dropdowns, I can select a rotation on X and Z axis of either 0 or 90 degrees. This rotation always has to be on the global axis so the user understands whats happening.

When i rotate the object 90deg on the X axis, everything is ok. When i try to rotate it by 90deg on the Z axis aditionally, the object is being rotated around the Y axis, not the Z axis.

I have tried to do with with setting transform.rotation directly, transform.Rotate and transform.RotateAround but nothing worked.

The first thing I do is always resetting the rotation to 0.

Object.transform.rotation = Quaternion.identity;

My Try with Eulers:

Object.transform.rotation = Quaternion.Euler(new Vector3(xRot, 0, zRot));

My Try with Quaternions:

Object.transform.rotation = Quaternion.AngleAxis(xRot * 90, Vector3.right) * Quaternion.AngleAxis(zRot * 90, Vector3.forward);

My Try with Rotate:

Object.transform.Rotate(new Vector3(xRot, 0, zRot), Space.World);

My expected result is the object to always rotate around the GLOBAL X/Z axis, no matter what the objects current rotation might be. But none of my tries resulted in the expected behaviour.

Upvotes: 1

Views: 754

Answers (1)

Yauheni
Yauheni

Reputation: 316

You can use this:

gameObject.transform.localEulerAngles = new Vector3(xRot, 0, zRot);

Or this for global axis:

gameObject.transform.eulerAngles = new Vector3(xRot, 0, zRot);

Upvotes: 2

Related Questions