Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by duckstape · Jul 15, 2020 at 07:41 PM · localrotation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duckstape · Jul 15, 2020 at 10:01 PM 0
Share

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: alt text

Is that possible? I mean if unity can show the axis I want to read from I should.

unbenannt.png (38.5 kB)
avatar image Bunny83 duckstape · Jul 17, 2020 at 11:10 AM 0
Share

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.

avatar image
2

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)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image duckstape · Jul 15, 2020 at 09:13 PM 0
Share

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.

avatar image Captain_Pineapple duckstape · Jul 17, 2020 at 07:55 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with changing global direction to local 0 Answers

Instantiating prefabs within the MeshFilter volume of object 1 Answer

Limiting rotation based on direction 0 Answers

How to control player camera on angled surface/independent of xyz axis? 1 Answer

TransformDirection is only working on Y Axis 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges