Sphere
Log in Join
☆ 8

godot navigationagent2d pathfinding stutters with 60 enemies

Godot 4.0.4 tower defense. I have up to 60 enemies each with a NavigationAgent2D following a path to the end. When a tower is placed the navmesh is rebuilt and all 60 call get_next_path_position() in the same frame, causing a visible hitch (about 50ms in profiler). Map is a TileMap with navigation layers baked. Two questions: should I stagger the path recalculation instead of all at once, and is there a built-in way to do that? Also the enemies sometimes cut corners through walls that aren't there - probably my navigation layer regions. Any pointers appreciated, I'm mostly self-taught on this.

⚑ Report

4 replies

☆ 0
F fern ·2026-11-06#1

Yes, stagger it. Don't let 60 agents recompute in one frame. Give each agent a random initial offset and only call path compute when agent.is_navigation_finished() or on a timer, spread over frames. Also avoid rebuilding the whole navmesh every time a tower is placed - update just the affected tile region if possible. What does your rebuild look like?

⚑ Report
☆ 0
P paper ·2026-11-08#2

I call NavigationServer2D.bake() on the whole map whenever a tower goes down. That rebuilds everything including the static border. Staggering with a random offset helped the frame hitch a lot - down to 12ms. The corner-cutting is because my navigation region is a big rectangle and I rely on agent avoidance. I think I need to actually carve out the blocked tiles from the region.

⚑ Report
☆ 0
C cron ·2026-11-10#3

Avoidance and navmesh are different systems - avoidance won't keep them out of walls, it only separates agents. You do need the navmesh to reflect obstacles. For a tilemap you can update specific nav regions or bake from the tilemap's navigation cells so blocked tiles are excluded. Lower avoidance radius if they're clumping, and only repath agents whose path is actually blocked.

⚑ Report
☆ 0
M moth ·2026-11-11#4

Ended up pre-baking the static map once and using NavigationServer2D's source geometry to add tower obstacles dynamically, then repath only agents whose path crosses the new obstacle. Way cheaper than a full rebake. Frame time is steady now. Corner cutting was fixed by actually using the tilemap navigation cells instead of one big polygon - turns out I was never feeding the walls into the bake at all.

⚑ Report
Log in to reply.