Tutorial 2: Creating Your First Dungeon
In this tutorial, we will use the Constraint Generator to build a structured dungeon made of hand-crafted room templates.
Prerequisites
- A few Room Prefabs (e.g., a 10x10 room, a 5x10 corridor).
- Tiles or sprites already placed in those prefabs.
Step 1: Prepare Room Templates
The generator needs to know where the doors are in your prefabs.
- Open your Room Prefab.
- Add the Room Template Component to the root GameObject.
- Create a child object for each door and add the Door Anchor component.
- Position the anchor exactly on the tile where the connection should happen.
- Ensure the forward vector (blue arrow) points outward from the room.
- In the
RoomTemplateComponentinspector, click Bake Template. This saves the room's boundary data.
note
Space for Screenshot: A prefab with Door Anchors visible and the Bake button in the inspector.
Step 2: Create a Dungeon Flow
A "Flow" defines the logical layout of your dungeon.
- Right-click in Project: Create > Dynamic Dungeon > Dungeon Flow.
- Name it
SimpleDungeonFlow. - Open the Dungeon Designer window.
- Right-click to add nodes:
- Start Node: Where the player begins.
- Room Node: A generic room.
- Goal Node: The end of the dungeon.
- Draw lines between them to define the path.
note
Space for Screenshot: A simple flow graph (Start -> Room -> Goal).
Step 3: Configure the Generator
- Create an empty GameObject in your scene named
DungeonSolver. - Add the Dungeon Generator component.
- Set Generation Mode to
Flow Graph. - Assign your
SimpleDungeonFlowasset. - In the Template Library, add the room prefabs you prepared in Step 1.
Step 4: Generate
- Click Generate in the inspector.
- The solver will attempt to fit your templates together according to the flow graph.
- If successful, the dungeon will appear as instantiated prefabs in your scene.
note
Space for Screenshot: The generated dungeon layout in the scene.
Troubleshooting
If you see "Generation Failed":
- Check that your Sockets match (e.g., both doors have the
SmallDoorsocket type). - Ensure you have enough variety in your template library (if the flow requires a 3-way branch but you only have linear corridors, it will fail).
- Enable Enable Diagnostics to see a step-by-step log of the solver's attempts.