Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 apixalET · May 23, 2014 at 11:26 PM · coroutinesstartcoroutinetransform.key-frame animationendless loop

Dynamically updating a Coroutine's parameters every frame

Hello!

How can we update the values of pointA & pointB every frame to feed into our coroutine?

These two coroutines are called on IEnumerator Start() which animate a square in an endless loop between two objects. Sometimes objectA and objectB move, and we need the square animation to update it's start and end coordinates every frame.

Right now, we get new coordinates fed at the beginning of the While loop, which gives us a messy transition. Thus, looking for a way to change them more frequently!

Thanks for the help in advance.

 public GameObject objectA;
 public GameObject objectB;
         
 IEnumerator Start()
 {
      while (true)
      {
           pointA = objectA.transform.position;
           pointB = objectB.transform.position;
         
           yield return StartCoroutine(startMove(transform, pointA, pointB, timeToPointB));
           yield return StartCoroutine(endMove(transform, pointB, pointA, timeToPointA));
      }
 }
 
 IEnumerator startMove(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
 {
      var i= 0.0f;
      var rate= 1.0f/time;
 
      yield return new WaitForSeconds (startPosPause);
 
      while (i < 1.0f)
      {
           i += Time.deltaTime * rate;
           thisTransform.position = Vector3.Lerp(startPos, endPos, i);
           yield return null;
      }
      yield return new WaitForSeconds (endPosPause);
  }

 IEnumerator endMove(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
 {
      var i= 0.0f;
      var rate= 1.0f/time;
 
      yield return new WaitForSeconds (startPosPause);
 
      while (i < 1.0f)
      {
           i += Time.deltaTime * rate;
           thisTransform.position = Vector3.Lerp(startPos, endPos, i);
           yield return null;
      }
      yield return new WaitForSeconds (endPosPause);
  }
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 rutter · May 23, 2014 at 11:38 PM

You can't change the arguments that were passed into the coroutine, but you could add some fields to your class and have the coroutine check those.

Example using args:

 IEnumerator DoStuff(int x) {
     while (x != 0) {
         //do something

         x--;
         yield return null;
     }
 }


Example using fields:

 int x;

 IEnumerator DoStuff() {
     while (x != 0) {
         //do something

         yield return null;
     }
 }

You have to be careful, though, as that second example wouldn't self-terminate. It would only end if some other piece of code told it to (by setting x to zero).

There are many ways you could organize this. The key idea is that you can allow other parts of your code to change those values, as long as they're in scope for both the coroutine and the other code. It's a bit risky if you're not careful.

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 apixalET · May 24, 2014 at 12:43 AM 1
Share

This totally got us on the right track! Thank you so much @rutter! We built in the two object's positions into our While loop and it works like magic. Here is how we changed the code:

In IEnumerator Start()

 while (true)
 {
      yield return StartCoroutine(start$$anonymous$$ove(transform, timeToPointB));
      yield return StartCoroutine(end$$anonymous$$ove(transform, timeToPointA));
 }

In the Coroutines

 IEnumerator start$$anonymous$$ove(Transform thisTransform, float time)
     {
          var i= 0.0f;
          var rate= 1.0f/time;
     
          yield return new WaitForSeconds (startPosPause);
     
          while (i < 1.0f)
          {
               i += Time.deltaTime * rate;
     
               Vector3 startPos = new Vector3 (objectA.transform.position.x, objectA.transform.position.y, objectA.transform.position.z);
                Vector3 endPos =  new Vector3 (objectB.transform.position.x, objectB.transform.position.y, objectB.transform.position.z);
     
                thisTransform.position = Vector3.Lerp(startPos, endPos, $$anonymous$$athf.SmoothStep(ease$$anonymous$$in, ease$$anonymous$$ax, i));
          }
     
     yield return new WaitForSeconds (endPosPause);
     
     }

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

WaitForSeconds doesn't seem to work. 1 Answer

Coroutines IEnumerator not working as expected 2 Answers

Coroutine runs Very Slow for First time Call 1 Answer

How to force Coroutine to finish 1 Answer

how to restart coroutine 2 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