I made my EnemyMover script a script that isnt part of my enemy prefab. Instead, it picks them up from the object pool and then moves them in a complex loop as shown below :
IEnumerator MoveEnemyToWaypoint()
{
for (int i = 0; i < path.Count; i++)
{
foreach (GameObject enemy in _enemies)
{
if (i == 0)
{
enemy.transform.position = path[0].transform.position;
}
var enemyCurrentPos = enemy.transform.position;
Vector3 startPosition = enemyCurrentPos;
if (i + 1 < path.Count)
{
Vector3 endPosition = path[i + 1].transform.position;
float travelPercent = 0f;
enemy.transform.LookAt(endPosition);
while (travelPercent < 1f)
{
travelPercent += Time.deltaTime;
enemy.transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
enemyCurrentPos = endPosition;
}
}
}
for (int d = 0; d < _enemies.Count; d++)
{
Destroy(_enemies[d]);
}
}
The problem I encounter is when my loop has more than one enemy to manage. It starts moving them one by one instead of all moving them independently at the same time like I hoped to. So my question is : is there a way to achieve this desired result this way? What modifications could help? Or if I should stick to how its shown in this class?
I wanted to upload two videos here I took to show the comparison between a single enemy or multiples but I cant, so basically with a single enemy just imagine that it moves just like in this class, smoothely until end of path.
With multiples, one will move to the next waypoint, then the other will do the same, etc. Like they’re all waiting for eachother before going for the next waypoint. I know this behaviour makes sense in a loop but I’m still asking to know if there’s a way to do it similar to this but have them all move at the same time.
Thank you.