I am currently working on an enemy in a multiplayer game. I want it to be able to detect if any of the players are in range of it and then move towards whichever player is closest. I am using the Mirror API for the multiplayer if that matters. With what I have already, the enemy will move towards one player, but once another player gets closer or the current player dies, the enemy will just stand still. Is there a more efficient way to do this or something I can fix?
What I have at the moment (I have this code in my update function):
int j = 0;
foreach (GameObject player in players)
{
if (Vector3.Distance(players[j].transform.position, transform.position) <= followDistance && isStopped == false)
{
EnemyMove();
}
else
{
j++;
}
}