Rotating TO certain angle while avoiding gimbal lock

Hello. I’m currently encountering gimbal lock issue in custom, in-game inspector that let you precisely set rotation of object, minus “precisely” part.

I can’t use Rotate* methods, because they’re rotating BY specific angle, which makes the tool useless for building levels for my game and while I try to use euler angles, the aforementioned gimbal lock occurs. I’ve looked through other solutions, but all require using Rotate* methods which as I said are useless to me as I need to set angles precisely.

You should be able to set the rotations using the Quaternion class (specifically maybe Quaternion.AngleAxis) and assign it directly to the rotation property.

@darkhog To rotate a certain number of degrees on one axis while preserving the original rotation, you must take the original rotation * the rotation calculated by Quaternion.AngleAxis().

float degreesToRotate = 25f;
transform.rotation *= Quaternion.AngleAxis(degreesToRotate , transform.forward);

This will rotate 25 degrees around the z-axis while preserving the original rotation, so if the original rotation is (23f, 24f , 20f), the new rotation would be (23f, 24f , 45f).