I came across a cool book on Maze algorithms and tried to create one in Unity.


I came across a book called Mazes for programmers by Jamis Buck. I can't remember why I was looking at generated mazes. I vaguely remember wanting to make a dungeon crawler like Orks & Elves (DS) / Hired Guns (Amiga). I had no idea how to create a 3D maze but after a little googling, I found Jamis's book.

I prepared myself for some heavy math.  What I got instead were well-written, easy to understand steps with clear illustrations. The examples are written in Ruby, but the book focuses on breaking down each algorithm into steps. Because of this, you can easily write them any language.

The book takes you through many popular maze generation types. I tried coding the "The Binary Tree" algorithm in Unity with C#. The Binary Tree works by visiting each cell (room) and making a choice between cutting a path North or East.

Below is a video of my attempt. I've slowed it down so you can see the maze being generated room by room. I made it a little more visually interesting by using a few lights a floating camera.

One issue with the Binary Tree is that you can end up with one long corridor. I had a go at trying to solve that issue but although it looks good, it's definitely got a lot of dead ends.

The book turned out to be a lot of fun. The maze above only took an hour or two to create. I'm looking forward to making more complex mazes as part of future projects.