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 ProgenitorVirus · Jan 11, 2015 at 06:09 PM · rotationrotatecoroutine

Coroutine Rotation Drifting/Imprecise

My script handling a 360-degree rotation over time on the Z-axis keeps drifting by about +/-2 degrees when complete, and I cannot figure out why this is happening. I have disabled all other of my movement/rotation scripts as I at first thought they were interfering but it didn't seem to fix the problem. Here's my script:

 public float rollDuration = 1.0f;
 
 private bool inRoll = false;
 
 void Update ()
 {
     if(!inRoll)
     {
         if(Input.GetButtonDown ("RollLeft"))
         {
             StartCoroutine("Roll", -1);
         }
         else if(Input.GetButtonDown ("RollRight"))
         {
             StartCoroutine("Roll", 1);
         }
     }
 }
 
 IEnumerator Roll(int direction)
 {
     inRoll = true;
     float time = 0.0f;
         
     Vector3 initialRotation = transform.localRotation.eulerAngles;
         
     Vector3 currentRotation = initialRotation;
         
     Vector3 targetRotation = new Vector3(currentRotation.x, currentRotation.y, currentRotation.z - (360.0f * direction));
 
         
     while(time < rollDuration)
     {
         currentRotation.z = Mathf.Lerp(initialRotation.z, targetRotation.z, time/rollDuration);
         transform.localRotation = Quaternion.Euler (currentRotation);
             
         time += Time.deltaTime;
             
         yield return null;
     }    
         
     inRoll = false;
 }


Any insight or help on this issue is greatly appreciated! :)

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
0
Best Answer

Answer by GameVortex · Jan 11, 2015 at 07:51 PM

You only rotate while time is less than rollDuration, so your roll will not get preciesly to the target rotation because you will never supply the Lerp function with the value higher or equal to 1. You need to do one more roll/lerp calculation the first frame that time >= rollDuration in order to get currentRotation to be equal to targetRotation.

The easiest way would actually be to set currentRotation to be equal to targetrotation after the while loop.

  while(time < rollDuration)
      {
          currentRotation.z = Mathf.Lerp(initialRotation.z, targetRotation.z, time/rollDuration);
          transform.localRotation = Quaternion.Euler (currentRotation);
              
          time += Time.deltaTime;
              
          yield return null;
      }    
 
 transform.localRotation = Quaternion.Euler (targetRotation);
Comment
Add comment · Show 1 · 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 ProgenitorVirus · Jan 11, 2015 at 09:24 PM 0
Share

Thank you very much for not only providing the solution, but a clear explanation as well! This solution worked absolutely perfectly :)

Cheers!

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

How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds? 1 Answer

Rotate an object 180 degrees smoothly in a particular direction 1 Answer

How to rotate an object around another 60 degrees with a keypress? 0 Answers

How to get all gameObjects with a script on them 1 Answer

Trouble rotating a prefab while it's moving up,Rotating an object with an animation while it moves up 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