- Home /
Gimbal Lock with Quaternions
What is the difference between these
1. ctrl.spawnedObject.transform.localRotation *= Quaternion.Euler( 0f, 0f, 90f );
2. ctrl.spawnedObject.transform.Rotate( new Vector3( 0f, 0f, 90f ), Space.Self );
3. ctrl.spawnedObject.transform.localRotation *= Quaternion.AngleAxis( 90f, Vector3.forward );
and this?
Vector3 worldVec = ctrl.transform.localToWorldMatrix.MultiplyVector( Vector3.back );
ctrl.spawnedObject.transform.RotateAround( ctrl.spawnedObject.transform.position, worldVec, 90f );
I have an object inside of a container. The object is spawned as prefab with a default Euler rotation of (90, 0, 180). I would like to rotate the object on the local Z axis. However, all rotations listed in the first code block result in a rotation on the local Y axis.
The second code block performs the expected rotation around the local Z axis.
I'd like to better understand why.
In the editor, I can see that the block is gimbal locked (rotating the Y, and Z parameters rotate the object on the Y axis) and the block's transform.forward reads as Euler (0, -1, 0).
I created an example project here: https://github.com/lrgsteven/help_unity_gimbal
Thanks for your help.
any of those approaches should work.
here's a simple demo project showing modifying localRotation via euler-angles.
--> are you sure the code is executing as you expect ? ie, are those lines actually executing ? put in some logging.
--> is the scene wired to the controller correctly ?
--> cubes are notoriously symmetrical under 90º rotations. are you sure the cube's not rotating ? I'd suggest using a less symmetrical model. I've often seen the letter "F" used.
Thanks for the reply!
a) I've stepped through the code with my debugger to check values.
b) As far as I can tell.
c) I will change the name of the cube in the example. It's actually a model of a block with distinct geometry.
It is heartening to hear that my approach seems correct and that something else may be the problem. I'll try to build a more simple example and report back within a couple of days. For now, I'm actually swamped with other work and this got bumped way down in priority.
I checked out the project you made and everything you're doing makes complete sense to me. I've rephrased the question because now I'd like to understand why the other stuff does not work.
Your answer