2D Navigation with TileMap(-Layers) runtime baking

Godot Version

GoDot 4.4.1 (.NET)

Situation

Hey guys,
I have very exhausting problems with navigation for a week now. I am working on a top-down 2D game. I have a player, user controlled, and an enemy, a NPC controlled by NavigationAgend2D.

If you need more details, please let me know.

Problem 1 - Detour

So the enemy (controlled by navigation agent) should go on the shortest way to my player. Sometimes everything is working fine, but sometimes the enemy’s path is going away from the player then he turns about 300° degrees and then going to player, still not on perfect way. Sometimes this path is going through navigation polygon holes.

Problem 2 - Baking error

I have 2 relevant TileMapLayers, the “Ground” layer and “Barrier”.
The tiles in Ground owns navigation polygons and the TileSet have the “Navigation Layer” 1.
The tiles in Barrier owns only collision and no navigation polygons.
The NavigationPolygon’s “Source Geometry Mode” of the NavigationRegion2D is “Group Explicit”. Only Ground and Barrier are assigned to this group. But when baking, I always get this error:

E 0:00:00:954   _build_step_find_edge_connection_pairs: Navigation map synchronization error. Attempted to merge a navigation mesh polygon edge with another already-merged edge. This is usually caused by crossing edges, overlapping polygons, or a mismatch of the NavigationMesh / NavigationPolygon baked 'cell_size' and navigation map 'cell_size'. If you're certain none of above is the case, change 'navigation/3d/merge_rasterizer_cell_scale' to 0.001.

What I tried

  1. Change the merge rasterizer cell scale to 0.001. => baking error still there
  2. Removed Barrier from navigation group => no baking error but path always through barriers, detour same as before
  3. Some more, I forgotten

Code

Baking:

public override void _Ready() {
	navRegion = GetNode<NavigationRegion2D>("Map/NavigationRegion");
	navRegion.BakeNavigationPolygon();
}

I fixed both problems now.
What I did:

Solution for Problem 1

NavigationAgend2D settings I have changed:

  • Path Postprocessing: Edgecentered
  • Simplify Path: true
  • Avoidance Enabled: true

Solution for Problem 2

I only updated the collision polygon of my obstacles. Now each obstacle has 4-6 vertices and not up to 15.
Radius of “Agents” in my NavigationPolygon of the NavigationRegion is 10px.

1 Like