Efficient way to make enemy stop following player

Hello,

I’m working on a 2d top down shooter for PC, and I’m looking for ideas on how to make enemies stop following the player if they get far enough away. I’d like to make sure the enemies get close to the player first, and then allow a chance for them to lose interest if the distance to the player exceeds a certain amount.

Two possible ways to do this have occurred to me, so I’d like a little advice on which to pursue. Or if you have another idea, let me know. There could potentially be a lot of enemies onscreen, so I’d like a fairly simple approach that isn’t going to cripple performance.

  1. Vector3.distance called every 5 - 10 seconds. First check the distance is under a certain amount, set a bool to record that enemy has gotten close. Then wait for the player to move away, and set another bool to allow the enemy to pursue another objective.

  2. Put a child object on enemy with a trigger collider. When the player enters the collider a bool is set to record that the enemy has gotten close to the player. When the player leaves the collider, another boolean is set to allow the enemy to stop following.

Thanks for reading!

Using a trigger collider worked well. Reliable, easy to implement, and no appreciable performance hit.