Up to date
This page is up to date for Godot 4.0
.
If you still find outdated information, please open an issue.
2D Navigation OverviewΒΆ
Godot provides multiple objects, classes and servers to facilitate grid-based or mesh-based navigation and pathfinding for 2D and 3D games. The following section provides a quick overview over all available navigation related objects in Godot for 2D scenes and their primary use.
Godot provides the following objects and classes for 2D navigation:
- Astar2D
Astar2D
objects provide an option to find the shortest path in a graph of weighted points.The AStar2D class is best suited for cellbased 2D gameplay that does not require actors to reach any possible position within an area but only predefined, distinct positions.
- NavigationServer2D
NavigationServer2D
provides a powerful server API to find the shortest path between two positions on a area defined by a navigation mesh.The NavigationServer is best suited for 2D realtime gameplay that does require actors to reach any possible position within an navmesh defined area. Meshbased navigation scales well with large gameworlds as a large area can often be defined with a single polygon when it would require many, many grid cells.
The NavigationServer holds different navigation maps that each consist of regions that hold navigation mesh data. Agents can be placed on a map for avoidance calculation. RIDs are used to reference the internal maps, regions and agents when communicating with the server.
- The following NavigationServer RID types are available.
- NavMap RID
Reference to a specific navigation map that holds regions and agents. The map will attempt to join changed navigation meshes of regions by proximity. The map will synchronize regions and agents each physics frame.
- NavRegion RID
Reference to a specific navigation region that can hold navigation mesh data. The region can be enabled / disabled or the use restricted with a navigationlayer bitmask.
- NavLink RID
Reference to a specific navigation link that connects two navigation mesh positions over arbitrary distances.
- NavAgent RID
Reference to a specific avoidance agent with a radius value use solely in avoidance.
The following SceneTree Nodes are available as helpers to work with the NavigationServer2D API.
- NavigationRegion2D Node
A Node that holds a NavigationPolygon resource that defines a navigation mesh for the NavigationServer2D.
The region can be enabled / disabled.
The use in pathfinding can be further restricted through the navigationlayers bitmask.
Regions can join their navigation meshes by proximity for a combined navigation mesh.
- NavigationLink2D Node
A Node that connects two positions on navigation mesh over arbitrary distances for pathfinding.
The link can be enabled / disabled.
The link can be made one-way or bidirectional.
The use in pathfinding can be further restricted through the navigationlayers bitmask.
Links tell the pathfinding that a connection exists and at what cost. The actual agent handling and movement needs to happen in custom scripts.
- NavigationAgent2D Node
An optional helper Node to facilitate common NavigationServer2D API calls for pathfinding and avoidance for a Node2D inheriting parent Node.
- NavigationObstacle2D Node
A Node that acts as an agent with avoidance radius, to work it needs to be added under a Node2D inheriting parent Node. Obstacles are intended as a last resort option for constantly moving objects that cannot be re(baked) to a navigation mesh efficiently. This node also only works if RVO processing is being used.
The 2D navigation meshes are defined with the following resources: