I have a path fallowing script and this is the coroutine that is called when a path has been generated (before calling it stops it as well). The problem is when enemy (the one with this coroutine) is going through one of the many paths and randomly decides to stop. Only way to get it unstuck is with pausing the game and unpausing (Ctrl+Shift+P) then it runs a bit and the stops again. All of the lines of code are fired when it’s stuck. However, what I have noticed is that the last line that tells enemy object to move doesn’t work even thought it is fired. I didn’t have this problem before while working on this project for a half a year. This is the Code for courotine:
IEnumerator FallowPath()
{
if (path.Length <1)
{
StopCoroutine("FallowPath");
}
else {
Vector2 currentWaypoint = path[0];
targetIndex = 0;
while (true)
{
if (transform.position.Equals(currentWaypoint))
{
targetIndex++;
if (targetIndex >= path.Length)
{
yield break;
}
currentWaypoint = path[targetIndex];
}
transform.position = Vector2.MoveTowards(transform.position, currentWaypoint, speed * Time.deltaTime);
yield return null;
}
}
}