Pathfinding: Unity NavMesh Components
DunGen offers built-in integration with Unity's modern, component-based NavMesh Components system (package com.unity.ai.navigation). This allows you to automatically generate or utilize NavMesh data for your procedurally generated dungeons at runtime, enabling AI navigation.
Separate Package Required
Unity's NavMesh Components system is not included with the Unity Editor by default (as of writing). You must install it via the Unity Package Manager (Window > Package Management > Package Manager). Search for AI Navigation or add it by name: com.unity.ai.navigation.
Prerequisites
Install the NavMesh Components package (com.unity.ai.navigation) into your Unity project using the Package Manager.
Setup
- Locate the GameObject in your scene that has the
Runtime Dungeoncomponent attached. - Add the
Unity Nav Mesh Adaptercomponent to this same GameObject (Add Component > DunGen > NavMesh > Unity NavMesh Adapter).
This adapter will automatically run during DunGen's post-processing phase after the dungeon layout is generated.
Adapter Component Settings
The Unity Nav Mesh Adapter component has several settings to control how NavMesh generation occurs:
-
Priority: Determines the order in which multiple DunGen adapters are run. Lower numbers run first. Usually, the default value is fine unless you have complex inter-adapter dependencies. -
Runtime Bake Mode: This crucial setting defines how the NavMesh data is generated or handled at runtime:Pre-Baked Only:- Requires: You must add a
NavMeshSurfacecomponent to each of your Tile prefabs and bake them individually in the Editor beforehand. - Functionality: DunGen will not generate any new NavMesh data at runtime. It assumes the necessary data already exists on the tiles. This is the most performant runtime option as no baking occurs. Use
Link Rooms(see below) to connect these separate surfaces.
- Requires: You must add a
Add If No Surface Exists:- Functionality: If a placed Tile prefab doesn't already have a
NavMeshSurface, the adapter will add one and bake it at runtime. Tiles that do have a pre-baked surface are left untouched. - Use Case: Useful if you want to pre-bake complex tiles but have simpler tiles baked dynamically.
- Functionality: If a placed Tile prefab doesn't already have a
Always Re-bake:- Functionality: Ignores any existing
NavMeshSurfacecomponents on Tile prefabs. Adds a newNavMeshSurfaceto every placed Tile instance that doesn't already have, and bakes every Tile's surface at runtime.
- Functionality: Ignores any existing
Full Dungeon Bake:- Functionality: Ignores any NavMesh data on individual tiles. After the entire dungeon layout is generated, it adds a single, large
NavMeshSurfacecomponent encompassing the whole dungeon and performs one bake operation. - Pros: Can result in a more seamless NavMesh, especially for complex layouts or large open areas within the dungeon. Simpler setup as you don't need surfaces on prefabs.
- Cons: Can be significantly slower than other modes, especially for large dungeons, as the entire bake happens at once.
- Functionality: Ignores any NavMesh data on individual tiles. After the entire dungeon layout is generated, it adds a single, large
-
Link Rooms:- Functionality: If checked, DunGen will automatically place
NavMeshLinkcomponents across connected doorways between adjacent Tiles. - Purpose: This is essential when using pre-baked NavMeshes (
Pre-Baked Onlymode) or tile-by-tile baking (Add If No Surface Exists,Always Re-bake) as it allows AI agents to pathfind between the separate NavMesh surfaces of each tile. - Ignored: This setting has no effect in
Full Dungeon Bakemode, as there's only one continuous surface.
- Functionality: If checked, DunGen will automatically place
-
Auto-Calculate Link Points:- If checked, DunGen will attempt to automatically calculate the start and end points of the link between rooms. If unchecked, the start and end points of the link will be a fixed distance from the doorway (see below).
-
Distance from Doorway:- Controls how far from the center of the doorway (on either side) the generated
NavMeshLinkstarts and ends. Adjust this based on your agent size and doorway geometry. Only relevant ifLink Roomsis checked.
- Controls how far from the center of the doorway (on either side) the generated
-
Agent Types Link Info:- Allows you to configure
NavMeshLinkgeneration settings on a per-agent-type basis (Agent Types are defined in Unity's Navigation settings). Add entries to this list to customize link generation. Agent Type: The specific agent type this link configuration applies to.Area: The NavMesh Area type assigned to the generated link (e.g., "Walkable", "Jump"). This allows different agents to treat traversals differently.- Only relevant if
Link Roomsis checked.
- Allows you to configure
-
Disable When Door is Closed:- Functionality: If checked, the generated
NavMeshLink's enabled state will be controlled by theIsOpenproperty of the Door Component found on any door prefab placed in the doorway. - Requires: Your door prefabs must have a script that correctly updates the
Doorcomponent'sIsOpenstatus when the door opens or closes. - Purpose: Allows doors to dynamically block or allow pathfinding routes. Only relevant if
Link Roomsis checked.
- Functionality: If checked, the generated
-
Auto-Generate Surfaces:- Visible Only: This setting only appears when
Runtime Bake Modeis set toFull Dungeon Bake. - Functionality:
- If checked, the adapter automatically creates a default
NavMeshSurfaceconfiguration for each Agent Type defined in your project settings during the full dungeon bake. - If unchecked, you must manually provide a list of pre-configured
NavMeshSurfacecomponents (e.g., on the same GameObject as the adapter) that will be used for the full dungeon bake. This gives you more control over the baking settings (voxel size, agent radius, etc.).
- If checked, the adapter automatically creates a default
- Visible Only: This setting only appears when
Handling Doors
As mentioned in the settings:
- Ensure
Link Roomsis checked (unless usingFull Dungeon Bake). - Ensure
Disable When Door is Closis checked. - Your door prefabs (used as Connectors/Blockers or via Lock & Key) must have a script attached that includes a
Doorcomponent and correctly sets itsIsOpenproperty totruewhen the door is open andfalsewhen it's closed.
DunGen will then automatically toggle the associated NavMeshLink component based on the door's state.
2D NavMesh Integration
DunGen also provides limited support for 2D NavMesh generation using Unity's NavMesh Components.
Use the Correct Adapter
For 2D projects, you must use the Unity NavMesh Adapter (2D) component instead of the standard Unity Nav Mesh Adapter.
The 2D adapter works similarly but specifically looks for the following components within your Tile prefabs to generate the NavMesh shape:
SpriteRenderer: Considers the sprite's mesh data. Uses the layers defined in the adapter's "Included Layers" setting to determine walkability (sprites on other layers are considered non-walkable).Tilemap: Considers tiles based on theirCollider Type:None: Considered walkable.SpriteorGrid: Considered non-walkable obstacles.- The shape is derived from the tile's mesh, not its collider shape.
Other settings on the 2D adapter generally mirror the standard adapter where applicable.
By using the appropriate Unity NavMesh Adapter, you can seamlessly integrate robust pathfinding into your DunGen-generated levels. Choose the Runtime Bake Mode and configure linking options carefully based on your project's needs and performance targets.