- Home /
LocalEulerAngles
I wrote a simple sphere rotation script, that allows my sphere to rotate by x and y axis. The problem is that I using transform.localEulerAngles and it doesn't seem to work properly. It looks like it is using global X and Y axis instead of local.
rotateGO.localEulerAngles = new Vector3(pitch, yaw, 0.0f);
P.S. found an answer https://answers.unity.com/questions/358731/localeulerangles-changes-global-angles.html but it only works with the Y axis. Is there any other way to do this?
Answer by Bunny83 · Sep 03, 2019 at 05:38 PM
Many people have the wrong idea about local spaces. The localEulerAngles specify the orientation of the object in the local space of the parent object, not relative to the objects own local space. This wouldn't even make any sense since the orientation of the object in it's own local space is always (0,0,0). Positions and rotations always have to be relative to some reference frame. Ultimately that's always the worldspace.
If an object doesn't have a parent the local and global position / rotation is the same. Euler angles specify the rotation of an object by carrying out 3 consecutive rotations on the object in a particular order. However there's no general valid order and there are 6 different arrangements. Unity's euler angles uses the order Y-X-Z around "local axis" or expressed in parent space it's the other way round Z-X-Y. From a global point of view the outermost gimbal is the y axis and the next one is the x axis. It seems what you want is the other way round. So you want to specify the x axis first and have rotate y relative too the x rotation.
Note that euler angles always specify absolute rotations. So they can not be used to rotate an object relative to it's current rotation. Keep in mind that if you want to allow actual relative rotation around two local space axis, the object will automatically rotate around z as well. Imagine you have an object that isn't rotated initially. If you first rotate 90° upwards around the x axis the object faces up. Now rotate 90° around the new local y axis to the right so the object is now looking right but tilted 90° clockwise. If you rotate back 90° around the local x axis you are back to the original direction but your object is still tilted 90° clockwise around local z. So without ever rotating around the z axis you end up with a z rotation. It's a bit like this just to visualize the effect better ^^.
So if you want to specify absolute rotation euler angles you have to know what rotation order you want. If the default order is not what you want, you can use a second intermediate gameobject which acts as seperate additional gimbal. That way you can rotate the parent on a certain axis and the child on another axis.
If you actually want to freely rotate with relative rotations (be aware of the implicit rotation around the third axis) you just can use two seperate calls of Rotate with the axis you want to rotate around.
Your answer
Follow this Question
Related Questions
Can't figure out local rotation axis. 1 Answer
Random Rotation To Animation HELP! 2 Answers
How to limit sprite rotation 0 Answers
Clamping objects localrotation not working (C#) 2 Answers
can't get local rotation of object 2 Answers