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 atomaton · Apr 01, 2014 at 07:53 AM · javascriptquaternionrotateeuleranglesgimbal-lock

Incrementing Rotation Produces Strange Transform Values

At this stage in my project I have a bridge the player is standing on. When he walks over a button, the bridge needs to rotate by 72 degrees (1/5 of a full rotation) around its local X axis, which coincides with the pillar supporting it. This interpolation needs to ease out and in.

The script I'm currently using works by rotating the bridge by a certain increment around its X axis every update until the desired rotation is reached or exceeded, at which point it stops rotating and snaps to the proper value.

The bridge starts with an x rotation value of 288, or 360 - 72 degrees. Its y rotation is 328.3 degrees, which is the value needed for the x axis to be pointing the right direction. It's z rotation is 0.

But there's an issue. The script works perfectly the first three times, and then goes haywire. It rotates smoothly to 360 or 0 degrees, and then to 72 degrees. THEN, when I rotate it the third time, as the x rotation reaches 90 degrees, the y and z rotations suddenly change even though I didn't tell them to, and the x rotation begins incrementing downwards instead of upwards. In the game view, the rotation continues uninterrupted and it snaps properly. But now its x rotation is no longer the expected 144 degrees. Instead, it ends up around 36 degrees. This causes problems in the following rotation.

When the rotating process is finished, my code tells the bridge to snap its x rotation to a value equal to its initial x rotation (set before each rotation begins) plus a specific value (in this case, 72). So when I rotate the bridge a fourth time, it rotates smoothly to the new position, but then snaps to 108 (36 + 72), which is completely wrong.

Here is the code. It's attached to the trigger that the player walks through.

         private var triggerTag : String = "Player";
         public var bridge : GameObject;
         public var character : GameObject;
         public var rotationSpeed : float = 5f;
         public var rotating : boolean = false;
         public var ROT : float = 72f;
         
         public var min : float = 0f;
         public var max : float = 10.0f;
         
         private var rotIncrement : float = 0f;
         private var currentRot : float = 0f;
         private var initialRot : float = 0f;
         private var rotAmount : float = 0f;
     
         
             
         function OnTriggerEnter (other : Collider) {
         if (other.CompareTag(triggerTag) && !rotating) {
             rotAmount = ROT;
             character.transform.parent = bridge.transform;
             initialRot = bridge.transform.localEulerAngles.x;
            }
         }
      
         
         function Update () {
         print (bridge.transform.localEulerAngles);
         
             if (currentRot < rotAmount) {
             rotating = true;
             } else {
             if (currentRot > rotAmount) {
             bridge.transform.localEulerAngles = Vector3(initialRot + rotAmount, 328.3, 0.0);
             character.transform.parent = null;
             Reset ();
             } else {
             rotating = false;
             }
             }
             
             if (rotating && (currentRot < (rotAmount)/2)){
                 rotIncrement += 0.01 * rotationSpeed * Time.deltaTime;
                 rotIncrement = Mathf.Clamp (rotIncrement, min, max);
                 bridge.transform.Rotate(rotIncrement, 0, 0, Space.Self);
     
             } else {
             if (rotating) {
                 rotIncrement -= 0.009 * rotationSpeed * Time.deltaTime;
                 rotIncrement = Mathf.Clamp (rotIncrement, min, max);
                 bridge.transform.Rotate(rotIncrement, 0, 0, Space.Self);
             }        
         }
         currentRot += rotIncrement;
     }
     
         function Reset () {
             rotating = false;
             rotAmount = 0f;
             currentRot = 0f;
             rotIncrement = 0f;
             initialRot = 0f;
         }

While looking around online, someone mentioned Gimbal Locking, which I am only semi-familiar with. Could this be causing the issue? I know that Quaternion functions avoid Gimbal locking, but I've been avoiding them because I want the rotation of my bridge to ease in and out smoothly, and I don't know of a Quaternion function that achieves this result.

Can anyone come up with a solution?

Here are some images in case the description wasn't clear enough:

After Second Rotation: alt text

After Third Rotation, when things get weird: alt text

bridge3.jpg (185.7 kB)
bridge4.jpg (195.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Dave-Hampson · Apr 04, 2014 at 11:34 AM

You can use a Quaternion Lerp ( https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html ) to smoothly interpolate between two orientations. Create a 'start' orientation for the object and a 'end' orientation. Then calculate the intermediate rotation using Lerp for increasing t values between 0.0 and 1.0

Comment
Add comment · 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

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

21 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

Related Questions

Per-Axis (Euler) Rotation Control While Avoiding Gimbal Lock 1 Answer

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

eulerAngles + quaternion + car = help!?! 2 Answers

Rotation of child object is different in play mode 0 Answers

How to rotate an object around another 60 degrees with a keypress? 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