I have created two coroutines, one for moving my badGuy game objects right and another for moving them left (please see code below).
IEnumerator moveBadGuyLeft (Transform fromPosition, Vector3 toPosition, float duration, int newIndex) {
while (emptyPos.Contains(badGuyPos[newIndex])) { //how to switch to moveBadGuyRight when this condition is false
emptyPos.Add(badGuyPos[newIndex + 1]);
filledPos.Remove(badGuyPos[newIndex + 1]);
float counter = 0;
Vector3 startPos = fromPosition.position;
while (counter < duration)
{
counter += Time.deltaTime;
fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
yield return null;
}
emptyPos.Remove(badGuyPos[newIndex]);
filledPos.Add(badGuyPos[newIndex]);
if (newIndex > 0) {
newIndex--;
}
startPos = toPosition;
toPosition = new Vector3 (badGuyPos[newIndex], startPos.y, startPos.z);
int waitInterval = Random.Range(3, 5);
yield return new WaitForSeconds(waitInterval);
}
}
IEnumerator moveBadGuyRight (Transform fromPosition, Vector3 toPosition, float duration, int newIndex) {
while (emptyPos.Contains(badGuyPos[newIndex])) { //how to switch to moveBadGuyLeft when this condition is false
emptyPos.Add(badGuyPos[newIndex - 1]);
filledPos.Remove(badGuyPos[newIndex - 1]);
float counter = 0;
Vector3 startPos = fromPosition.position;
while (counter < duration)
{
counter += Time.deltaTime;
fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
yield return null;
}
emptyPos.Remove(badGuyPos[newIndex]);
filledPos.Add(badGuyPos[newIndex]);
if (newIndex > 0) {
newIndex++;
}
startPos = toPosition;
toPosition = new Vector3 (badGuyPos[newIndex], startPos.y, startPos.z);
int waitInterval = Random.Range(3, 5);
yield return new WaitForSeconds(waitInterval);
}
}
I am trying to move my badGuy objects left till they meet a filled position then move them right till they meet a filled position and so on, toggling between left and right. I know that if my condition
while (emptyPos.Contains(badGuyPos[newIndex]))
is false then I should change from one coroutine to the other or vice versa. How can I implement this changing between coroutines? Please see how I am calling the coroutine in the Start method below:
for (int i = 0; i < badGuys.Count; i++) {
if (badGuys [i].getBlockType () == BadGuySetup.BadGuyType.moving) {
int indexInBadGuyPos = badGuyPos.IndexOf(badGuys[i].getBadGuyGameObject().transform.position.x);
Vector3 targetPos = new Vector3(badGuyPos[indexInBadGuyPos - 1], badGuys[i]. getBadGuyGameObject().transform.position.y, 0.0f);
StartCoroutine(moveBadGuyLeft(badGuys [i]. getBadGuyGameObject().transform, targetPos, 1.0f, indexInBadGuyPos - 1));
}
}
UPDATE
I added the following to the if (newIndex) > 0
condition in moveBadGuyLeft
and moveBadGuyRight
:
in moveBadGuyLeft
:
if (newIndex > 0) {
newIndex--;
if (!emptyPos.Contains(badGuyPos[newIndex])) {
isMovingLeft = false;
badGuyDestPos = new Vector3(badGuyPos[newIndex + 2], startPos.y, startPos.z );
badGuyNewIndex = newIndex + 2;
break;
}
}
in moveBadGuyRight
:
if (newIndex > 0) {
newIndex++;
if (!emptyPos.Contains(badGuyPos[newIndex])) {
isMovingLeft = false;
badGuyDestPos = new Vector3(badGuyPos[newIndex - 2], startPos.y, startPos.z );
badGuyNewIndex = newIndex - 2;
break;
}
}
Then I created another coroutine which is suppose to change between the two coroutines:
IEnumerator movingBadGuys(Transform fromPosition, Vector3 toPosition, float duration, int newIndex) {
if(isMovingLeft){
yield return StartCoroutine (moveBadGuyLeft(fromPosition, toPosition, duration, newIndex));
}
else if(!isMovingLeft){
yield return StartCoroutine (moveBadGuyRight(fromPosition, toPosition, duration, newIndex));
}
}
Finally I updated looping through the badguys and moving them:
for (int i = 0; i < badGuys.Count; i++) {
if (badGuys [i].getBlockType () == BadGuySetup.BadGuyType.moving) {
int indexInBadGuyPos = badGuyPos.IndexOf(badGuys[i].getBadGuyGameObject().transform.position.x);
Vector3 targetPos = new Vector3(badGuyPos[indexInBadGuyPos - 1], badGuys[i]. getBadGuyGameObject().transform.position.y, 0.0f);
badGuyDestPos = targetPos;
badGuyMoveDuration = 1.0f;
badGuyNewIndex = indexInBadGuyPos - 1;
StartCoroutine(movingBadGuys(badGuys [i]. getBadGuyGameObject().transform, badGuyDestPos, badGuyMoveDuration, badGuyNewIndex));
}
}
But this is changes are not working. I am testing it with only two badGuys which always start moving left and they only move left once and stop though there are other empty positions for them to continue moving left.