Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Daggett110 · Mar 09, 2017 at 10:26 AM · rotationscripting problemeditor

Unable to efficiently read rotation euler values as feedback

Hello, I have been trying to make a vr game where you tighten bolts by rotating a wrench around them. I had no idea how intricate rotations really are. I'm tring to make it so a bolt only rotates on its local x axis to follow a hand. I accomplished this using hinge joints and the appearance is great. The issue comes when measuring the turn so only clockwise motion is effectively tightening a bolt. I assumed it would be as easy as "when x rotation increases (righty tighty) tighten the bolt (represented by a health bar for the bolt) and when it decreases (lefty loosey), loosen the bolt (replenish health)."

I discovered that in the editor, when a hinge joint goes past 180, it loops back to 0 so I simply said if the absolute value of the rotation since last update is above arbitrary value, subtract 360 to have it represent the visual change in degree and vice versa.

my code to accomplish this is as follows:

     [Range(0f, 100f)]
     public float health = 100f;
 
     float prevRot;
 
     float rotChange;
 
     float degToHealth = 360f / 100f;
 
     private void Awake()
     {
         // Put in some starting value
         prevRot = transform.localRotation.eulerAngles.x;
     }
 
     void Update ()
     {
         rotChange = transform.localRotation.eulerAngles.x - prevRot;
 
         Debug.Log(transform.rotation);
 
        // Handles any issues with rotation rolling around to 0 when reaching 360
         if(rotChange > 300 || rotChange < -300)
         {
             rotChange = rotChange > 300 ? rotChange - 360 : rotChange + 360;
         }
 
         // remove health as necessary
         health -= rotChange / degToHealth;
 
         // bind the health
         if (health >= 100)
             health = 100;
         else if (health <= 0)
             health = 0;
         
         // store the previous rotation to use in the next fixed update loop
         prevRot = transform.localRotation.eulerAngles.x;
     }

My issue is that when I try to read these values and represent the health, they are different when reading them from the transform and what is displayed in the editor.

For example the editor reads rotation of my "wrench" as (152, 0, 0), but reading from transform.localRotation.eulerAngles.x gives me (38, 180, 180). I understand it is flipping the other two axes, and showing the distance from even ground, but why? Why is it different from what is shown in the editor? and how do I correct for this when making my calculations for health?

Link to gif of issue (couldn't post a longer gif without losing legible framerate and quality) https://plus.google.com/photos/photo/112004969329323323975/6394542961768124482?icm=false&authkey=CNX8s5Pqr7_iEw

Comment
Add comment · Show 1
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 _dns_ · Mar 09, 2017 at 04:09 PM 0
Share

Hi, Unity stores rotations as quaternions. The euler angles values are just one representation of this quaternion in many possibles representations. I mean, there are usually multiple combinations of euler angle that can represent the same rotation. Unity is quite consistent in it's way of converting one into the other but it's always something one must be aware of (and use quaternions as much as possible)

The other complexity is the local or global (world) space. In your example, you compare the angles from the inspector that are in world space with the angles from the local space of a transform (= the rotation from it's parent object point of view, not from the world point of view). I'm sure if you display transform.rotation.eulerAngles you'll have the same values as the inspector.

For your problem, it may be easier to compare the angle in the local space as the rectangle object is rotating around 1 aligned axis (it's parent's transform's x axis i guess from the gif).

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I keep my character facing the direction of travel after movement stops? 1 Answer

Script works in Game View, but not in build 0 Answers

Can I know why I can't control volume of audiomixergroup without exposing parameters? 0 Answers

Rotating cube steadily around world axis not working properly 0 Answers

cover more distance in the same time to keep gameObjects connected when rotating 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