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);
}