Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1369580 Posts in 64364 Topics- by 56384 Members - Latest Member: multimedia.capitalworks

November 22, 2019, 12:45:08 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Procedural Dungeon Generation in Unity
Pages: [1]
Print
Author Topic: Procedural Dungeon Generation in Unity  (Read 12610 times)
uniquecorn
Level 0
**


do med packs help with crippling depression?


View Profile WWW
« on: June 19, 2014, 10:41:35 PM »

I've been working on my roguelike for sometime now and created my own dungeon generation algorithm for it so I'd like to share some of the technical details. There are a bunch of algorithms out there and different methods for building dungeons. My method creates rooms, then corridors to link them up.

Step 1: Generate rooms.

I first create a room prefab which is a empty gameobject with a collider. It has a script to randomise its scale of x and z. The script will also create a floor and 4 walls around the gameobject.



I have a dungeon class to spawn these rooms around a set distance. The older rooms destroy the newer rooms if they overlap to ensure that the rooms are properly placed.



Step 2: Create corridors

Link up the rooms by spawning a corridor prefab that scales it's length with "roomA.x - roomB.x" or "roomA.y - roomB.y". In most cases you will need 2 corridors for each link, one horizontal and one vertical. Depending on your type of dungeon you might want to consider the rooms height and length as well. You could generate it from origin to origin, or generate it from edge to edge. Also, by calculating both x any y lengths, you can determine whether the rooms need 1 or 2 corridors to link up.



You also have to consider what will happen if corridors overlap, which blocks will be destroyed and which will be kept, but for a basic dungeon you can allow your corridors to simply overlap by destroying the walls of an overlapped corridor. That way the corridors will merge like crossroads, or merge into a long hallway. However the floor tiles will be overlapping and you have to destroy one of the corridors overlapped floors. If you destroy both, there will be a hole in your corridor.



I generate a small alcove at the point where corridors meet to help it link better.



Step 3: Random architecture

Adding extra secret rooms and corridors that have to be activated and such. I added depth to my dungeon by lowering and elevating certain corridors while adding steps.



It's pretty important to make sure that your dungeon doesn't become messed up by blocked corridors and stuff like that. Start small first and slowly add more features. Also add a small character or something to explore the dungeon and make sure that your features don't break anything.

« Last Edit: June 19, 2014, 11:08:38 PM by uniquecorn » Logged

aberrantmike
Level 0
**



View Profile WWW
« Reply #1 on: June 22, 2014, 04:23:40 PM »

looks siiick.
Logged
Code_Assassin
Level 5
*****


freedom


View Profile
« Reply #2 on: June 22, 2014, 07:23:12 PM »

Looks simple and easy to implement. Initializing prefabs is cool, but it can really clutter up your scene hierarchy. When doing procedural maps I find it helpful to create a cohesive gameObject with child/parent relationships like so:

Map
   .../Rooms
       .../Walls
       .../Stairs
       .../Traps
       ... so on and so forth

where map is an empty game object.

Keeps things nice and tidy.
Logged
uniquecorn
Level 0
**


do med packs help with crippling depression?


View Profile WWW
« Reply #3 on: June 23, 2014, 12:11:56 AM »

Looks simple and easy to implement. Initializing prefabs is cool, but it can really clutter up your scene hierarchy. When doing procedural maps I find it helpful to create a cohesive gameObject with child/parent relationships like so:

Map
   .../Rooms
       .../Walls
       .../Stairs
       .../Traps
       ... so on and so forth

where map is an empty game object.

Keeps things nice and tidy.

Yes! Keeps the hierarchy clean and also you can use the StaticBatchingUtility to batch parent gameobjects.
Logged

Si
Level 0
*


View Profile
« Reply #4 on: June 23, 2014, 04:17:37 PM »

Awesome.

Logged
Lycaon
Guest
« Reply #5 on: June 25, 2014, 03:41:30 PM »

Looks simple and easy to implement. Initializing prefabs is cool, but it can really clutter up your scene hierarchy. When doing procedural maps I find it helpful to create a cohesive gameObject with child/parent relationships like so:

Map
   .../Rooms
       .../Walls
       .../Stairs
       .../Traps
       ... so on and so forth

where map is an empty game object.

Keeps things nice and tidy.

Yes! Keeps the hierarchy clean and also you can use the StaticBatchingUtility to batch parent gameobjects.


Yeah! I do this kind of thing with all my unity projects. Player stuff under an empty game object, level-related stuff under an empty game object, etc. Very useful.
Logged
uniquecorn
Level 0
**


do med packs help with crippling depression?


View Profile WWW
« Reply #6 on: June 10, 2015, 09:33:19 PM »

It's been a while since I last posted here! I've completely changed my dungeon generation algorithm to be faster and more flexible. The previous method required a lot of destroying of blocks and instantiation of thousands of objects. I've since used pooling and made dungeon creation almost instantaneous.

First thing that was wrong with my generation code was it relied on triggers to destroy blocks. This is heavily unoptimised as it meant every block having colliders. Instead I switched to a chunk reliant algorithm.



I split the game world into chunks of 5 x 5 units.



Then I start generating rooms. First I allocate an unused chunk with an origin room. Then I give the room a random chunk offset on it's edges, giving it a width and a height. I generate another room, randomizing it's offsets and checking if chunks overlap via math instead of colliders. I also give rooms a default unused offset so that rooms will never touch.



Corridor generation is pretty much the same, but with chunks instead, so it's cleaner and no destroying. When I place down corridor chunks I check if the chunk is occupied first, therefore, there is no need to destroy any blocks, and corridors overlap very nicely.



Fleshed out with blocks placed, the dungeon will look a bit like this. You have to prepare 5x5 blocks to place down of course, but they can be randomised, giving each room a unique look.

Walls are still a problem for me currently, I'm trying to find a method that will make creating walls easier and more efficiently. Right now, I'm just culling walls that are beside one another using a distance and orientation check.



With art assets in this is how my game currently looks. The big room is a pre made set of chunks but using the same offset guide.
Logged

indie11
Level 2
**


View Profile
« Reply #7 on: June 10, 2015, 10:34:23 PM »

Nice article :D

Got some questions for you..

Do you use pre-made rooms? Or you generate them randomly with a maximum width and height?
Do the rooms have fixed entry/exit points(points from which they are connected to other rooms)?
Logged

uniquecorn
Level 0
**


do med packs help with crippling depression?


View Profile WWW
« Reply #8 on: June 10, 2015, 11:12:34 PM »

Nice article :D

Got some questions for you..

Do you use pre-made rooms? Or you generate them randomly with a maximum width and height?
Do the rooms have fixed entry/exit points(points from which they are connected to other rooms)?


I use both! Some pre made rooms and some rooms that have a random maximum width and height. Every wall on a room's edge is removable by a corridor, even pre made rooms. Pre made rooms can have random elements to them as well, like lighting, props and mobs. There are no set entry and exit points to a room. However, I did create a boss room and placed it on the outside of the grid which created a long corridor that led to a pre made the entrance.

Logged

indie11
Level 2
**


View Profile
« Reply #9 on: June 10, 2015, 11:35:41 PM »

Also, why are you using a 2d array  as a base structure? Is it because of path finding? What if there we use another constraint to limit the map, lets say the number of rooms.

I found out that unity doesn't supports Dynamic Navmesh so which approach are you using? And how are you implementing the movement of NPCs?
Logged

uniquecorn
Level 0
**


do med packs help with crippling depression?


View Profile WWW
« Reply #10 on: June 11, 2015, 12:22:45 AM »

Also, why are you using a 2d array  as a base structure? Is it because of path finding? What if there we use another constraint to limit the map, lets say the number of rooms.

I found out that unity doesn't supports Dynamic Navmesh so which approach are you using? And how are you implementing the movement of NPCs?

the 2D array is to handle generation with data instead of actual objects, so generation is faster. After I've gotten the layout of the dungeon in the array I place down the 3d models to create the actual dungeon.

Pathfinding is handled with Astar pathfinding, after the dungeon is created I scan it at runtime to create an astar map.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic