- Home /
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);
}
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.
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
Follow this Question
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