# Use a single rotation axis of a freely rotating object

Hello,

I’m currently working on a Virtual Reality project. The controllers, as expected, have all degrees of freedom in rotation. In the world I have a fixed object that I want to rotate accordingly to the rotation on the x-axis of the controller.
Simply put: you rotate the controller, the object rotates aswell but only over the x-axis.

The general problem is the calculations of Quaternions and euler angles and I can’t wrap my head around it. The code I started off with is as follows:

``````transform.localEulerAngles = new Vector3(usable_controller.transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
``````

Except this locks the rotation to 180 degrees (Gimbalt lock?).

Some explanation and an example would be greatly appreciated.

edit:
To further explain what I’m trying to accomplish I’ve made a video which you can view here. I want the controller on it’s flatside to be able to turn the red handle a 180 degrees overhead from right to left and back. The thing I’m going for here is that the controller will be fixed on a handle in real life. This way the user has to turn an actual handle while also viewing a handle being turned in VR.

Avoid using euler angles whereever possible. The eulerangle representation is just calculated from the current quaternion rotation and as you said eulerangles might suffer from gimbal lock. Furthermore Eulerangles have a fix order in which the 3 rotations are applied. In Unity it’s Z - X - Y around world space axis (or Y - X - Z in local space axis given you start at rotation 0,0,0)

Just use quaternions. If you want to rotate around worlds space axis you can do

``````transform.rotation = Quaternion.AngleAxis(xDeg, Vector3.right) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(yDeg, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(zDeg, Vector3.forward) * transform.rotation;
``````

If you want to rotate around the local space axis of an object you can do:

``````transform.rotation = Quaternion.AngleAxis(xDeg, transform.right) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(yDeg, transform.up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(zDeg, transform.forward) * transform.rotation;
``````

Note that xDeg, yDeg, zDeg are relative rotation angles. So a xDeg value of “20” will rotate the object from it’s current orientation 20 degree around the given axis. If you have trouble to understand quaternions and want to know more about them, see this Numberphile video

@Bunny83
That would be a good solution if I want to rotate the object around an axis by itself. I’m now just not sure what value to assign to xDeg. Simply writing the x-rotation of the controller to the variable does not work.

To further explain what I’m trying to accomplish I’ve made a video which you can view here. I want the controller on it’s flatside to be able to turn the red handle a 180 degrees overhead from right to left and back. The thing I’m going for here is that the controller will be fixed on a handle in real life. This way the user has to turn an actual handle while also viewing a handle being turned in VR.