Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 False_Idolitry · Jun 14, 2012 at 04:25 AM · rotationtransformeuleranglesquaternions

Min-Angle Constraint Script Causes Jitter

The relevant snippet is as follows:

 public void LateUpdate()
  {
         float error = 0.0f;
         float localZRot = transform.localRotation.eulerAngles.z;
 
         if (Mathf.Sign(transform.localRotation.w) < 0)
             localZRot = localZRot - 360;
 
         if (localZRot < MinAngle)
         {
             error = MinAngle - localZRot;
         }
         else if (localZRot > MaxAngle)
         {
             error = MaxAngle - localZRot;
         }
         transform.Rotate(0.0f, 0.0f, error);
  }

The script, as part of a MonoBehavior, is intended to limit the local rotation of a transform between two angles. Every LateUpdate, the function:

  • grabs the local rotation about the Z axis in the form of a Euler angle value [0, 359],

  • uses the sign of the quaternion's w component to determine whether the angle is to the left or right of 0 (that is, clockwise or counter-clockwise from Quaternion.Identity),

  • checks if the angle violates the minimum or maximum angle constraints and finally,

  • rotates the transform by the difference between the constraint and the actual rotation.

The function actually works as intended, rotating the transform back to the minimum or maximum angle whenever it violates the constraint. However, at random intervals, when satisfying the minimum angle constraint, the transform will jitter and its rotation will momentarily appear to be equal to the MaxAngle constraint before flashing back to the appropriate MinAngle. The inability to reliably reproduce this error given the same input makes me think this is some sort of funky side effect of using LateUpdate, but the docs make it seem like LateUpdate is the function to use if I want to override any changes to my transform that other calculations may have made. My questions are:

1. Is there anything in my code that might be causing this?
2. Is the fact that this function body appears in LateUpdate() the cause of my issue?
3. If so, how else can I reliably enforce rotational constraints on a transform?

Thanks in advance for the help!

EDIT: To make things even weirder, I've found that explicitly setting localRotation to a quaternion representing the MinAngle or MaxAngle rotation works perfectly, but the aforementioned Quaternion.Rotate() method causes the associated jitter. What is the reason for this? Is Quaternion.Rotate() not instantaneous?

For clarity, this is the updated function code that works flawlessly with no noticeable jitter:

 public void LateUpdate()
 {
     float error = 0.0f;
     float localZRot = transform.localRotation.eulerAngles.z;

     if (Mathf.Sign(transform.localRotation.w) < 0)
         localZRot = localZRot - 360;

     if (localZRot < MinAngle)
     {
         transform.localRotation = Quaternion.AngleAxis(MinAngle, Vector3.forward);
     }
     else if (localZRot > MaxAngle)
     {
         transform.localRotation = Quaternion.AngleAxis(MaxAngle, Vector3.forward);
     }
 }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getting the actual object rotation on just 1 axis 1 Answer

How to Rotate a CUBE by 90 Degrees when a key is pressed 1 Answer

"Object reference not set to an instance of an Object" 0 Answers

Rotation/position question. 1 Answer

EulerAngles value as floating point 1 Answer


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