It’s procedural maze generation time!

Hello everybody!

Recently I’ve been working on a procedural maze generator in Unity 3D. It’s been coming along excellently and it’s even in a playable demo right now.

Screenshot from the playable demo

Screenshot from the playable demo

Anyway, I’m writing this post to release the package! There’s not much to it, and I don’t have a YouTube video explaining yet (coming soon, probably), but the package can be located here. It’s pretty close to ready to run out of the box, simply create an empty and attach the MazeGenerator script to it. It comes with a couple prefabs that you can place on the inspector for the script (Outer Walls/Walls/Ceiling/Floors). Then, after that’s all set up all you’ll have to do is hit run and it’ll generate the maze starting wherever starting coords is.

You can also hook into the script to get a list of the rooms using something like:

_mazeScript = (MazeGenerator) GameObject.Find("MazeGenerationObject").GetComponent(typeof (MazeGenerator));
var rooms = _mazeScript.Rooms;

After that, then you have a C# List<int[]> of Rooms. The integer array is always 5 length (I know this is a terrible way to do this, I’m sorry) and those are indexed as follows:

0: Starting X
1: Starting Z
2: Y index
3: Length of room (Length = X)
4: Width of room (Width = Z)

So, for example, the code I use for placing a player in a random room is as follows:

var randomRoom = Random.Range(0, _mazeScript.Rooms.Count);
var floatArray = _mazeScript.Rooms[randomRoom];
if (floatArray != null)
var minX = floatArray[0];
var minZ = floatArray[1];
var y = floatArray[2];
var maxX = minX + floatArray[3];
var maxZ = minY + floatArray[4];
Instantiate(Player, new Vector3((minX + maxX)/2, y+1, (minZ + maxZ)/2), new Quaternion(0,0,0,0));

(I need a better code formatter for WordPress…)
Anyway, you should get the point by now. If you have any questions/comments/concerns, leave a message and I’ll try to address it ASAP. I’ll try to make the YouTube video go into depth and try to show you some of the stuff I’m doing.

Thanks for reading!


This entry was posted in Scripts and tagged , , , , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback.

Leave a Reply