Skip to main content

Navigation in Godot

References in the official docs:

Other places to learn from:

  • This tutorial series by GDQuest covers steering behaviours. Itโ€™s written for Godot 3, but the principles still seem solid (still working through it). Youโ€™ll have to mentally translate from Godot 3 code to Godot 4 code in some areas.
  • A server holds different navigation maps. Each map consists of multiple regions. Each region holds navigation mesh data.

Everything seems to interact with the navigation server. NavigationRegion2D is just a helper to define a region.

Obstacles do not impact pathfinding. They only impact avoidance. A navigation agent will still attempt to pathfind directly through an obstacle. If your agent doesnโ€™t have avoidance enabled, then obstacles do nothing.

Defining Navigation Regionsโ€‹

A NavigationPolygon can be used to define an area that can be navigated to. This is used internally within NavigationRegion2D.

Tilesets use NavigationPolygons internally for navigation layers:

The TileSet Editor creates and uses this resource internally when defining tile navigation areas. [Source]

So, based on this, if you want to put an area in a tilemap area that a navigation agent should path around, then you need to have a tile that handles this. You canโ€™t put a node on top that has a NavigationObstacle on it and expect your navigation agent to find its way around it.

Navigation regions need to be baked before they can be used.

When working with scene collections on your tile map, each scene in the tile set needs to have its own NavigationRegion2D created with the navigable regions defined. You donโ€™t paint them on in the Tile Set drawer like you do with an atlas. Additionally, also set the Agent Radius to 0 so that the agent can pass over into the new region, or just make the region big enough that itโ€™s a non-issue ยฏ\_(ใƒ„)_/ยฏ.

Geometry on a navigation polygon can be defined by drawing points on your 2D scene.

How the polygon gets baked depends on its configuration:

  • Navigation decides what should remove areas from the navigation polygon.
    • Parsed Geometry Type defaults to Meshes and Static Colliders.
      • A mesh counts as one of Polygon2D, MeshInstance2D, MultiMeshInstance2D, or a TileMap. The tile map one means that any scenes used in a scene collection are treated as a mesh, regardless of what kind of scene it is!
      • A static collider counts as a StaticBody2D or a TileMap collider. (I think this refers to โ€œUse Kinematic Bodiesโ€ being enabled, but Iโ€™m not sure).
    • Parsed Collision Mask will only show up if weโ€™re considering static bodies. This controls which 2D layers will be considered. A node on Layer 3 will be ignored if this collision mask disables Layer 3.
    • By default, only nodes that are children of the navigation region will be considered for the navigation polygon. This can be worked around by setting Source Geometry Mode to either Group With Children or Group Explicit.