- Home /
can't get local rotation of object
So the code for reading the z-rotation look like this:
valText.text = "z local Val: " + transform.localEulerAngles.z.ToString() + " z global Val: " + transform.eulerAngles.z.ToString();
And I'm rotating the object on the local y-axis but it says the local z-axis rotates, too. Is it because the object has no parent. If yes, how can I get the values of the same axis, which the local rotate-gizmo shows? Here is the Video.
Answer by Bunny83 · Jul 15, 2020 at 09:04 PM
Well there are many misconceptions of local space / local axis / local rotation / local position.
First I think it's important to clear this up first. Every gameobject creates / represents it's own local space. This space is represented by it's local axis. However the local position and the local rotation of an gameobject are of course not relative to it's own coordinate space. That wouldn't work since the local position and rotation defines that coordinate space. The local position / rotation defines the object's position and rotation relative to the parent object. So those two values are defined in the parents local space.
Objects which do not have an explicit parent are implicitly a child of the world. That means the localPosition is equal to the world space position. Likewise the localRotation / localEulerAngles are equal to the worldspace rotation / eulerAngles.
So the first thing we can already get from this is that the localEulerAngles of an object does not represent rotations around it's own local axes but around the parent axes.
Next thing is euler angles is not one thing. Euler angles are 3 consecutive rotations applied to the object in a certain order. Just assume that the object is originally aligned with it's parent (so it has a localEulerAngles of 0,0,0). Unity uses the rotation order Z -> X -> Y around the parent axes. Note that this is equivalent to the opposite rotation order around the object's local axes. Since your object seems to be tilted around X, when you rotate around the local Y axis you actually change all 3 angles at once.
If you want a relatively "clean" rotation around a certain axis you should use a parent that is actually tilted so you can rotate around a single axis of the parent.
Finally a warning about reading eulerAngles / localEulerAngles. Unity does not work with euler angles internally since eulerAngles have several issues. Unity, like most gameengines, uses Quaternions to represent rotations. Quaternions do not represent the orientation by 3 seperate consecutive rotations but as a single operation. A quaternion defines an arbitrary rotation axis and the amount to rotate around that axis. Unity actually calculates the eulerangles representation backwards from the quaternion rotation. Though his is a problem because euler angles do not have a unique representation of a certain orientation. Therefore two or more euler angles combinations can result in the same orientation. That means when the euler angles are calculated from the quaternion Unity will not necessarily end up with the same kind of euler angles representation. So using euler angles to read the orientation could fail in several edge cases. The most well known edge case where eulerangles completely fail is a gimbal lock configuration
Thank you for your answer. But I don't want to set a tilted axis. I want to read it. In summary I want to read this axis:
Is that possible? I mean if unity can show the axis I want to read from I should.
This axis? Do you mean the object's rotation around that axis? Well unfortunately this is the first rotation in the 3 consecutive rotations. That means assume you have a rotation of 20° around y. If the object is tilted around x as well by for example 45°, the direction of the up axis is now pointing into the 200° direction since it's now tilted backwards at 20°. This assumes the unrotated forward axis as reference. If you now apply a z rotation (the last in the hierarchy) the y axis will also tilt again and it points into a different direction. So the question is if you want to know the rotation around that axis, what is your reference? where is 0°? What if you approach gimbal lock when you rotate x to +- 90°. In this case your whole orientation has flipped to the side. The y and z rotations will simply add up or cancel each other. Since the axis is no lying horizantally, where's 0° now?
You can get a more coherent value when you change your gimbal order so your desired axis is the last one that is rotated from the the local space perspective.
So it's completely unclear what reference frame you have in $$anonymous$$d. When the object has an arbitrary rotation in space you can not use the objects own space as reference since that would always be 0° since that IS the orientation of the object anyways.
Answer by Captain_Pineapple · Jul 15, 2020 at 08:54 PM
No, if an object has no parent the local rotations/positions equal their global counterparts.
You face mutliple problems here: First up: You kind of misunderstood how the angles display a relative rotation. Basically you get a clean change of you axis angle when the axis is aligned with the corresponding axis of your parent transform. (If there is no parent it's the global transform)
Secondly: For each possible rotation there are multiple euler variants to describe this rotation. For this reason you can not rely on euler angles to keep specific values even though you rotate around one specific axis. This means that when reaching a certain angle unity might flip signs on one axis and add some values on the others just on a whim. (While still maintaining the exact same rotation)
So, how do I solve my problem? I want the z value not to change if I rotate the x and y axis on the local rotate tool.
$$anonymous$$ost easy way would be to introduce a parent transform which you rotate in a way that it's y-axis is always aligned with your objects y-axis. This way you can read a "clean" y-rotation. Same can then be done with another parent transform for the x-axis.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003252im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Quaternion lerp is not rotating my object to the desired rotation? 1 Answer
Rotating CharacterController around object pivot when character controller has offset? 0 Answers
Finding a local position matching a global position in an individually rotated hierarchy 2 Answers
Rotation Script bug - rapidly alternating between positive and negative rotation 1 Answer