I'm doing a post-mortem on my game project, and this problem kept bugging me as my team couldn't solve it throughout.

So my game is a pure 2D top-down shooter built with Unity3D, with enemies constantly spawning and pathing to the player. The top-down shooter has narrow passages making it somewhat maze-like, and for now I can have anywhere from 1 to 20 enemies in-game at any point in time. I use a 2D pathfinding package which I assume to be sufficiently optimized.

Currently it performs terribly thanks to the pathfinding algorithm being called too many times.

What tricks can I use to reduce the number of calls?

PS. I know I could use NavMesh and all that built-in awesomeness. But the way we had set the game up denied us the use of those features :/

  • \$\begingroup\$ The first question to ask: did you run the AI on each and every frame? Or was it only like, once per second or something like that? \$\endgroup\$
    – Vaillancourt
    Feb 5, 2016 at 2:44
  • \$\begingroup\$ My first implementation ran it on every frame. I tried to optimize by running if my player moved some distance but that didn't work too well either. \$\endgroup\$
    – Mark Ang
    Feb 5, 2016 at 8:27
  • \$\begingroup\$ My answer to this question should be relevant: gamedev.stackexchange.com/a/106512/41345 \$\endgroup\$
    – Evorlor
    Feb 7, 2016 at 14:52
  • \$\begingroup\$ My answer to this question may also be relevant: gamedev.stackexchange.com/a/124339/2472 \$\endgroup\$
    – amitp
    Jun 24, 2016 at 3:34

2 Answers 2


Instead of pathing from each new enemy to the player as they spawn of the player moves significantly. Do a floodfill from the player to fill the entire map with how far the player is from each tile and which way he is. Then for pathing you just follow the arrows.

The floodfill Will only need to update the neighbours of the tiles whose value changed after the player moved. And you can limit how many tiles get updated per frame.

As another point you may need to double check your heuristic that you gave the package. A* is only as good as its heuristic.


You probably run the pathfinding algorithm every frame. It should only happen once, when the enemy spawns. If your area is destroyable, then you will need to do it every time an obstacle is destroyed.

  • \$\begingroup\$ as the player moves, pathfinding would have to update. players typically move every frame \$\endgroup\$
    – CobaltHex
    Feb 5, 2016 at 20:35
  • \$\begingroup\$ you could update more often if the enemy is closer to the player and less often if far away. \$\endgroup\$ Mar 9, 2019 at 1:31

Not the answer you're looking for? Browse other questions tagged .