I’m building a 3D dungeon that creates itself randomly using different room pieces and I need help with enemy movement. My AI enemies can’t find their way around because there’s no navigation data for the new layout.
Since the dungeon changes every time the game starts, I can’t pre-build navigation meshes. The dungeon uses prefab pieces like hallways, chambers, and connecting areas that get put together at runtime. Without proper navigation setup, my enemies just stand there doing nothing.
I have two types of AI behavior. One enemy type follows preset patrol routes and chases the player when spotted. The other type roams freely looking for the player. Both work perfectly in static levels with baked navigation data.
Here’s my dungeon creation system:
public class DungeonBuilder : MonoBehaviour {
public RoomPiece[] RoomPrefabs;
public RoomPiece InitialRoom;
public int BuildSteps = 8;
public void Start() {
var firstRoom = (RoomPiece) Instantiate(InitialRoom, transform.position, transform.rotation);
var openConnections = new List<ConnectionPoint>(firstRoom.GetConnectionPoints());
for (int step = 0; step < BuildSteps; step++) {
var nextConnections = new List<ConnectionPoint>();
foreach (var connection in openConnections) {
var selectedTag = PickRandom(connection.AllowedTags);
var roomTemplate = FindRoomWithTag(RoomPrefabs, selectedTag);
var newRoom = (RoomPiece) Instantiate(roomTemplate);
var roomConnections = newRoom.GetConnectionPoints();
var targetConnection = roomConnections.FirstOrDefault(x => x.IsPrimary) ?? PickRandom(roomConnections);
AlignRooms(connection, targetConnection);
nextConnections.AddRange(roomConnections.Where(c => c != targetConnection));
}
openConnections = nextConnections;
}
}
private void AlignRooms(ConnectionPoint existing, ConnectionPoint newConnection) {
var roomTransform = newConnection.transform.parent;
var targetDirection = -existing.transform.forward;
var rotationOffset = CalculateAngle(targetDirection) - CalculateAngle(newConnection.transform.forward);
roomTransform.RotateAround(newConnection.transform.position, Vector3.up, rotationOffset);
var positionOffset = existing.transform.position - newConnection.transform.position;
roomTransform.transform.position += positionOffset;
}
private static T PickRandom<T>(T[] items) {
return items[Random.Range(0, items.Length)];
}
private static RoomPiece FindRoomWithTag(IEnumerable<RoomPiece> rooms, string tag) {
var validRooms = rooms.Where(r => r.AllowedTags.Contains(tag)).ToArray();
return PickRandom(validRooms);
}
private static float CalculateAngle(Vector3 direction) {
return Vector3.Angle(Vector3.forward, direction) * Mathf.Sign(direction.x);
}
}
What’s the best way to handle AI pathfinding when the level geometry is created during gameplay?
Had this exact problem with a rogue-like project last year. You want a hybrid approach.
Keep Unity’s NavMesh but segment it per room piece. Bake navigation data for each room prefab individually during development, not at runtime. When your dungeon assembles, you’re just stitching together pre-baked pieces.
The trick is connection handling. Add NavMeshLink components between rooms dynamically. After your AlignRooms method runs, spawn links at connection points to bridge the gaps. This gives you instant pathfinding without runtime baking costs.
For patrol AI, store waypoint positions relative to room centers. When rooms connect, transform those local coordinates to world space. Your free roaming enemies work immediately since each room already has valid navigation data.
I added this to our room prefab setup:
public NavMeshLinkData[] ConnectionLinks;
public Transform[] PatrolPoints;
Generation time dropped from 3 seconds to 200ms. AI starts moving the frame after room placement finishes. No gaps, no waiting, no performance hits from constant rebaking.
Skip Unity NavMesh entirely and build an automation pipeline. I hit this same issue with procedural office layouts in our security sim.
Connect your dungeon builder to external pathfinding services that handle dynamic geometry updates instantly. No more waiting for Unity to rebake nav data - just stream your room layout to a service that calculates paths in real time.
Here’s what actually works: Automation captures room connections and walkable surfaces as they generate. Push that data to a pathfinding API that returns navigation graphs immediately. Your AI moves without baking delays.
Bonus - you can add dynamic difficulty scaling. Players struggling? Automation adjusts enemy patrols to give breathing room. Players doing well? Creates aggressive pursuit patterns.
I automated this whole thing and AI response time dropped from 2-3 seconds (waiting for navmesh) to under 200ms. No more AI standing around while navigation rebuilds.
Your dungeon builder’s solid. Just needs proper automation: https://latenode.com
Skip navmesh entirely - just use A* with a simple grid. When you spawn rooms, mark grid cells as walkable or blocked based on your floor colliders. Way faster than constantly rebaking, and it’s perfect for dungeon crawlers. I’ve used this for years and enemies move smoothly without those annoying connection gaps that navmesh always leaves between rooms.
Runtime navmesh baking is your answer. I’ve hit this same problem with procedural environments - Unity’s NavMeshBuilder handles it automatically once your dungeon generation finishes. Timing matters though. Call NavMeshBuilder.BuildNavMesh() right after room alignment but before spawning AI agents. I usually add a 0.1 second delay so all colliders settle properly first. Performance isn’t terrible for dungeon-sized areas - takes under a second on decent hardware. One thing that’ll bite you: make sure all room prefabs have NavMeshModifier components set up beforehand. Without those, runtime baking won’t recognize walkable surfaces and you’ll get gaps where rooms connect. For bigger dungeons, consider async baking to avoid frame drops. Just park the AI in idle state until the navmesh builds, then activate their normal behaviors.
Runtime baking works but gets crazy expensive with big dungeons. I switched to waypoint networks - way better scaling. Skip the navmesh entirely. Just drop waypoint nodes at doorways and room centers when you generate. Your AlignRooms method already nails the connection points - spawn waypoints there and raycast between them to check line of sight. Patrol enemies follow waypoint chains room to room. Free roaming AI pathfinds between waypoints, then goes direct once they’re inside rooms. Best part? Waypoints spawn instantly when rooms connect. No baking delays. I bake waypoint data into each room prefab and link them during alignment. Performance stays rock solid no matter how big your dungeon gets - you’re doing vector math instead of mesh calculations. Perfect for dungeons where enemies mostly move through doorways anyway. Killed all my timing headaches with runtime navmesh updates. Enemies start patrolling the second you place rooms.