Procedural tile generator?

Hey, everybody! I’ve got a few simple questions regarding a project I’m sort of self-teaching myself. I’ll begin by explaining that everything I know about Unity is self-taught. I haven’t had any form of formal education regarding scripting or video game design. I’m working, at this moment, on a script or sort of “Master” entity that will take my map tiles and build off of them accordingly.

Here’s a preview of what my scene looks like when pulled apart.

The tileset is composed of hallway segments, intersections, and corners. Based upon the settings I give it, it’ll branch out, give the number of “splits” in a hallway, branch those out, and have a random chance to “turn” during each section.

This is a breakdown of my map after I’ve pressed Play.

Starting at the first Hallway Segment in the North-Western corner of the map, Floor Master will begin building South to begin the first Split. It counts the number of segments in a Hall. It’ll go out that far and roll a set chance to turn. Chance failed, it has created 0 out of 4 intersections so far, it placed an intersection. Flip a coin. Heads right, tails left. Tails. The hallway that branches to the West is now a dead end. It turns 2 out of 2 times and promptly ends in a locked door.

The East branch of Intersection 1 counts a new hallway and succeeds a random check to turn. Add turn 1 of 2. It adds a new Hall going South and fails the second check at the end. Placed 1 out of 4 intersections so far, placed an intersection. Flip a coin. Tails. The hallway that branches to the West is now a dead end. It turns 2 out of 2 times and promptly ends in a locked door.

The East branch of Intersection 2 adds a small hallway (due to random chance) and fails its first check to turn. Added 3 of 4 intersections so far, adding an intersection. Flip a coin. Tails. The Southern branch of Intersection 3 is now a dead end. It turns 1 of 2 times and promptly ends in a locked door.

The North branch of Intersection 3 adds a new hallway, succeeds to turn and adds Turn 1 of 2; Now going East. Another hallway is added and the check to add a new turn fails, Placed 3 of 4 intersections, adding the final intersection. Since it is the final intersection, both branches are dead ends. The North branch ends in a locked door after a single hallway. The South branch turns 1 of 2 times and ends in a locked door.

Here’s an overview of my inspector settings and what they look like.

Now begs my question and current conundrum. The maps are placing hallway segments inside of other hallway segments. The hallways are building themselves fine. Despite some larger maps building into one another, I’m impressed. It works exactly how I’ve coded it on the first try. Now, my initial thought would be to make incoming hallway segments travel up or down to go over or under existing hallway segments. This gives the illusion of multiple “Floors” in a level. However, I can’t think of any simple ways to detect the existence of a hallway segment by another hallway segment. At the moment, I have a Physics.OverlapSphere that detects if there’s more than one Hallway Segment in the area, but it won’t seem to work properly.

Can anybody else think of any ideas regarding object detection? How would I make these hallways avoid one another? Could I eventually add this to my rooms and make the hallway segments avoid rooms entirely as well? Is there a better way to do this? Discuss it with me!

I didn’t really understand the details of your implementation but I’m currently planning out something very similar and the way I want to try out first is separating the data structure that the procedural generation works with from the game objects that get distributed in the end. So I’d use a grid, make a cell of the grid an object, let the cells know what their neighbors are and then implement rules to put data into the cells. I want to create something city-like so I’d start with a very big cell size for a first iteration and give the cells data for different “city blocks”. Then I’d create grids of cells with finer resolution for the city blocks and start creating buildings there and if necessary I could subdivide the grid once more to distribute detail objects or break up the buildings into modules or something like that. Then going through the generated data and distributing objects in the scene would be a separate step. To make that work with your hallways you need make all your segments conform to a defined grid size to be sure everything fits together nicely. You might want to add a third dimension to your data structure / cell array so that you can implement multiple floors. I’d imagine it to be a lot easier to block 2 neighboring cells in level 0 and the two above them in level 1 and then put a staircase there that snaps to the grid instead of doing complicated collision checks with your geometry.
Just some theoretical thoughts, none of it has been tested by me yet.

Let me know what parts of it you don’t understand and I’ll elaborate to the best of my ability.

I thought about designing a grid-like method of organizing the hallways, but had little experience trying something like that. I think I’ll start playing with it and see what I can achieve.

So I put a little more work into that idea. Each “Cell” in your grid is a node with a 10x10x10 Gizmo.DrawWireCube. It has 4 points on either 4 outer faces of the cube. Directional landmarks. You decide what object the segment will show, spawn a new one, and then place it on one of those 4 nodes according to the direction this segment is facing.

Here, you can see I’ve added some GUI elements to make troubleshooting a little easier. There’s a couple redundant buttons at the moment. That’s because I’m deprecating the “Turn Hallway Left” and “Turn Hallway Right” buttons, as they have been replaced by the R+ and R- buttons.

I’ve found a way to get the objects to find the appropriate node when you pass over one. Essentially, when you create a new empty cell, it first runs a check on the new location. It’s going to use GameObject.FindGameObjectsWithTag(“Map Segment”) to check all the existing cells to see if any of them has the same location as the desired new cell. If not, place the cell. If yes, place the cell at Y+1.

My next test is going to be discovering an existing piece and converting them into stairwells, which I’ve already crated for the map.