# 04 — Enemies

Add enemy spawn points, bake a NavMesh so enemies can move, and switch the exit to a monster-locked door.

Prerequisite: [02-simple-room.md](02-simple-room.md).

---

## Enemy spawn markers

1. Right-click on the root object → **Create Empty**
2. Rename it `EnemySpawn`
3. Place it on the floor, away from walls
4. Repeat — place **4 to 8** spread across the room

The game picks spawn points at random based on difficulty, avoiding points too close to the player.

> **Tip**: never place a spawn point inside geometry or directly in front of the door — enemies can block the exit.

---

## NavMesh — enemy pathfinding

Enemies use Unity's built-in **NavMesh** to navigate. Without one, enemies spawn but stand frozen — they cannot walk.

### Why custom rooms need their own NavMesh

The game's built-in rooms are all part of the same scene. Their NavMesh is baked once at build time and covers all that geometry. Custom rooms are instantiated at **runtime** from your bundle — they are not part of the scene when the NavMesh was baked, so they are not covered.

DKReforged solves this by loading any `NavMeshData` asset found in your bundle and calling `NavMesh.AddNavMeshData()`, adding your room's walkable surface to the active NavMesh at runtime.

> **NavMeshAgent** is the component on enemies that makes them follow the nav surface. The game's enemies already have it — you never touch it. You only need to provide the surface data.

### Bake the NavMesh (built-in, no extra package)

1. Make sure your scene contains your room geometry (floor, walls) placed normally in the scene hierarchy
2. Open **Window → Navigation**
3. In the **Bake** tab, click **Bake**
4. Unity creates a `NavMesh.asset` file inside a folder named after your scene (visible in the Project panel)

> Walls and ceilings are automatically excluded — only surfaces flat enough to walk on are included.

### Include NavMeshData in the bundle

1. In the Project panel, find `NavMesh.asset` (in the folder named after your scene)
2. Select it → at the bottom of the Inspector, open the **AssetBundle** dropdown → set it to your bundle name (e.g. `my-pack`)
3. Export the bundle again — the NavMesh is now packed with the room

When DKReforged loads the bundle, it finds the `NavMeshData` asset and registers it with `NavMesh.AddNavMeshData()` automatically.

---

## Monster-locked door

With enemies in the room, switch from `UnbarredDoor` to `MonsterLock`:

1. In the Hierarchy, expand `WhatLockIsNeeded`
2. Delete the `UnbarredDoor` child
3. Right-click on `WhatLockIsNeeded` → **Create Empty** → rename `MonsterLock`
4. Optionally add visual geometry inside `MonsterLock`:
   - Iron bar Cubes, a gate mesh, or any visual that represents a closed gate
   - These objects become **visible** (active) once all enemies are dead — they signal "exit is open"

> `MonsterLock` starts **inactive** and is activated by the game when all enemies in the room are dead. The player then walks through the opening to proceed.

---

## Hierarchy after this tutorial

```
my-pack_MainRoom
├── ... (geometry, lights)
├── SpawnHere
├── EnemySpawn  (×4–8)
├── LootSpawner (×5)
└── WhatLockIsNeeded
    └── MonsterLock
        └── [optional gate geometry]
```

The `NavMesh.asset` is a **separate asset** in the bundle — not a child of the room object.

---

## Next

→ [05-trap-room.md](05-trap-room.md) — add traps
