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 The702Guy · Aug 04, 2021 at 04:27 AM · coroutinerotate object

Coroutine not starting

So I'm new to coroutines and I'm really hoping I'm just missing something simple but I can't figure this problem out. I have an object I want to rotate but I don't want it to rotate instantaneously, I want it to rotate over a certain amount of time. In another post I found a bit of code that is supposed to accomplish what I'm looking for through a coroutine but it doesn't seem to be working. The object does not rotate at all. If I use the normal transform.rotate the object will rotate but does so instantly which is not what I want.

    GameObject objectToRotate;
     public float speed = 1f;
     public Vector3 rotationAmount = new Vector3(0, 0, 180);
 
     [SerializeField] private List<GameObject> platformsMoved;
 
     private void Start()
     {
         objectToRotate = platformsMoved[0];
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Bullet"))
         {
             clickSource.PlayOneShot(switchClick);
             StartCoroutine(MovePlatforms());
         }
     }
 
     IEnumerator MovePlatforms()
     {
         float endTime = Time.time + speed;
         float step = 1f/speed;
         var fromAngle = objectToRotate.transform.eulerAngles;
         var targetRotation = objectToRotate.transform.eulerAngles + rotationAmount;
         float t = 0;
         while(Time.time <= endTime)
         {
             t += step * Time.deltaTime;
             objectToRotate.transform.eulerAngles = Vector3.Lerp(fromAngle, targetRotation, t);
             yield return 0;
         }
     }
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
1
Best Answer

Answer by Bunny83 · Aug 04, 2021 at 05:22 AM

Well, first of all your coroutine is over complicated. While using eulerAngles is not recommended, this should generally work. Your coroutine could be just:

 static IEnumerator RotatePlatform(Transform objectToRotate, Vector3 relativeRotation, float duration)
 {
     var fromAngle = objectToRotate.eulerAngles;
     var toAngle = objectToRotate.eulerAngles + relativeRotation;
     Debug.Log("Rotate platform from " + fromAngle + " to " + toAngle, objectToRotate);
     for(float t = 0f; t < 1f; t += Time.deltaTime / duration)
     {
         objectToRotate.eulerAngles = Vector3.Lerp(fromAngle, toAngle, t);
         yield return null;
     }
 }
 
 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Bullet"))
     {
         clickSource.PlayOneShot(switchClick);
         StartCoroutine(RotatePlatform(objectToRotate.transform, rotationAmount, speed));
     }
 }

There are many things we don't know about your setup. Though keep in mind that StartCoroutine starts a coroutine on the monobehaviour here you run StartCoroutine on. So the coroutine is bound to that object. If that object is destroyed of deactivated your coroutine will be stopped / terminated.


Note that I added a Debug.Log statement for debugging to the coroutine. See if you can see this log in the console. If not, your trigger does not Fire at all.


Note I've refactored a few things. Your "speed" actually represents a "duration" which is pretty much the opposite of what a speed represents. A higher value means it takes longer. You should never yield the value "0". This will produce garbage as an integer value returned as object need to be boxed. When you want to wait for one frame, just yield null. I also renamed the coroutine since it doesn't move platforms but it rotates a single one ^^. Using class member fields to pass information to methods is bad design. By passing the object to rotate, the rotation amount and the duration as parameters the method is self-contained (so I made it static). This makes it way easier to reason about the code.


Finally keep in mind when this code is supposed to rotate a platform, this platform needs to have a kinematic rigidbody if it should interact with other rigidbodies.


ps: The debug log call has a second parameter which represents a context object. I passed the objectToRotate as context. When you click on the debug message in the console, Unity will ping / highlight that object in your project / hierarchy. Maybe you're trying to rotate the wrong object?

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 The702Guy · Aug 05, 2021 at 12:01 AM 0
Share

Thank you for the in depth explanation, it helped me understand it a little better. The debug.log is what helped me solve my issue. My previous code worked the same as yours, although yours is cleaner, but it turns out me declaring the Vector3 and initializing it outside of the start function was the issue. With the debug statement it was saying it was rotating to (0, 0, 0) which wasn't what the Vector3 was set too, as soon as I initialized it in the start function it worked properly.

Thank you again for your help, sometimes I forget that a simple debug statement is all I need to help solve my issue.

avatar image Bunny83 The702Guy · Aug 05, 2021 at 08:56 AM 0
Share

Note that your "rotationAmount" is declared as a public variable and therefore is serialized in the inspector. So it doesn't matter what you set the variable to in the field initializer. Of coruse overwriting the serialized value in Start would work, however if you set it from code it should not be serialized as this would be confusing. By overwriting it in Start the serialized value will become pointless instead.

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

132 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 avatar image

Related Questions

How to start a coroutine from Update. 4 Answers

Coroutine does not seem to work 1 Answer

Coroutine couldn't be started becuase inactive 0 Answers

coroutine spamming webserver which then stops replying 2 Answers

Yield WaitForSeconds Not Working. Coroutine. 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