Tower defence targeting first enemy
So I've been struggling for a while trying to figure out a way to implement this mechanic. Essentially my towers have a global range (not radius targeting) I had previously just used:
enemies = GameObject.FindGameObjectsWithTag("Enemy");
Which I could just take enemies[0] and use that to target... However, I have now added different enemy types. One of which can move faster than the rest. So this method no longer works.
I've researched what other people have done, and most people seem to do something like using a GameObject at the finish line and simply calculate which enemy is closest to that using Vector2.Distance(). However this will not work for me as I have a "Z" shaped path for all enemies to follow.
private GameObject GetFirstEnemy(GameObject[] enemies)
{
return enemy;
}
The best solution I can come up with seems overly complicated for what it should be. I was thinking I could have each enemy keep track of their current waypoint progress with either a list of bools or an int that just increments. Then in my GetFirstEnemy() method I pull that variable:
for (int i = 0; i < enemies.Length; i++)
{
enemies_waypoint_progress[i].waypoints_enemy_has_visited =
enemies[i].GetComponent<EnemyBehaviour>().GetVisitedWaypoints();
}
I would then have to find a way to filter out which enemies had gone to the furthest checkpoints (which I'm struggling to do) and then calculate the distance between those filtered enemies.
Can anyone come up with a better solution to this that mine? Thank you in advance.