Skip to content

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

  1. Locate the GameObject in your scene that has the Runtime Dungeon component attached.
  2. Add the Unity Nav Mesh Adapter component 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 NavMeshSurface component 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.
    • 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.
    • Always Re-bake:
      • Functionality: Ignores any existing NavMeshSurface components on Tile prefabs. Adds a new NavMeshSurface to every placed Tile instance that doesn't already have, and bakes every Tile's surface at runtime.
    • Full Dungeon Bake:
      • Functionality: Ignores any NavMesh data on individual tiles. After the entire dungeon layout is generated, it adds a single, large NavMeshSurface component 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.
  • Link Rooms:

    • Functionality: If checked, DunGen will automatically place NavMeshLink components across connected doorways between adjacent Tiles.
    • Purpose: This is essential when using pre-baked NavMeshes (Pre-Baked Only mode) 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 Bake mode, as there's only one continuous surface.
  • 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 NavMeshLink starts and ends. Adjust this based on your agent size and doorway geometry. Only relevant if Link Rooms is checked.
  • Agent Types Link Info:

    • Allows you to configure NavMeshLink generation 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 Rooms is checked.
  • Disable When Door is Closed:

    • Functionality: If checked, the generated NavMeshLink's enabled state will be controlled by the IsOpen property 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 Door component's IsOpen status when the door opens or closes.
    • Purpose: Allows doors to dynamically block or allow pathfinding routes. Only relevant if Link Rooms is checked.
  • Auto-Generate Surfaces:

    • Visible Only: This setting only appears when Runtime Bake Mode is set to Full Dungeon Bake.
    • Functionality:
      • If checked, the adapter automatically creates a default NavMeshSurface configuration 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 NavMeshSurface components (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.).

Handling Doors

As mentioned in the settings:

  1. Ensure Link Rooms is checked (unless using Full Dungeon Bake).
  2. Ensure Disable When Door is Clos is checked.
  3. Your door prefabs (used as Connectors/Blockers or via Lock & Key) must have a script attached that includes a Door component and correctly sets its IsOpen property to true when the door is open and false when 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 their Collider Type:
    • None: Considered walkable.
    • Sprite or Grid: 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.