• Register
Post tutorial RSS How to create a randomly generated maze

A Maze Kit is a Level Design plugin for Unity3D that allows you to draw and generate mazes and other paths from pre-made rooms. It includes prefabs so you can start right away to draw mazes and use them in your games, but it’s up to you to improve or replace them so you’ve got a personal touch on the final result. This first tutorial shows how to generate and draw mazes with the kit. Next tutorials will focus on how to create game from the included themes !

Posted by on - Intermediate Mapping/Technical

First, you need to know where to find this kit : it's available from Unity Asset Store directly and from Unity Magic.

Import the package once you've downloaded it. Once it's done, you should have a folder named «EquilibreGames» in your project view, and a folder «AMazeKit» in it (and no error in the console view that relates to this particular package of course).

Create a randomly generated maze
Create the maze object

Now that you have a fully functional package, let's create a random maze so you see its simple power in action !

Click on Create then A Maze in the Hierarchy view.

The newly created maze GameObject named aMaze is automatically selected and you can see a green cube on the scene view.

The description of this maze is stored in the folder Mazes in the Assets folder.

Tips : Focus the whole object with the «F» shortcut if you don't see the same as below.

In the Scene View, you can see the bounds of the maze. This is where the maze will be and where you can interact with it. They are updated when room are added or removed. They do not shrink though unless you remove floors (force the bounds to be entirely recalculated).

Configure the algorithm
The Depth First Search is the first algorithm to be included in the package. It has three parameters :

  • size : choose how many rooms there will be for each axis X, Y and Z
  • next choice : decide how will be chosen the next available room to be search during the algorithm.
  • openness : open more ways for a percentage of rooms (new in 1.1)

If you want to have a 2D maze, just put 1 on the Y axis, there will be only one floor, that's the simplest maze to solve.
If, on the contrary, you want a 3D maze, put anything you want ! Remember though it uses memory, and the generation uses every coordinate. That means a 10x20x5 maze will have 1000 rooms ! That's a lot to navigate !

Next choice
This choice is used when the algorithm searched for a room where to dig a hole : it always pick the choice you decide : the first, the last one, a random one, or the one in the middle of all choices it got.

Here's a sample on a 2D maze so you can see the effects.

This percentage of rooms tells the algorithms to create other issues to created rooms. The greater it is, the more the maze will be an «opened» maze : multiple paths will be created.

Launch the algorithm

Click on the Clear & Generate button to generate your new maze.

Depending on the size and your hardware, it can be instantaneous of take some time ! There's some progress bar to help you figure out what it does :

  • remove the old objects (if needed)
  • generate the maze from the algorithm
  • create the visual cubes for visual feed back

Remark : With a large maze, Unity may take a few seconds after the script has ended to display the cubes and let you handle it again.
Hint : The little «prefs» buttons on the first line select the settings asset for the algorithm in the project view.

Here's a sample generated maze with a size of (6, 3, 6) and random chosen as next choice, with an openness of 0%.

You can now edit the maze, generate a new one of build the maze with Prefabs.

Instantiate Prefabs

Click on Instantiate Game Objects

The maze is now fully generated and available to play !
Here's a sample of a generated maze with the default "SimplePack" theme.

Draw manually a maze
Know how to use the 3D view
Before drawing stuff, you have to know how the tool uses the 3D view. To reset the maze object, click on the red cross on the "Floors" title in the inspector of the maze game object.

From your point of view, it will decide wether you want to draw on the XZ plane or the XY plane. You'll see that from a 2D colored plane inside the green wireframe box.

With the default point of view, you'll see a blue plane, on the bottom side of the green wireframe box. This is the ground floor, and the plane is the XZ plane representing the surface where you can draw.

If you watch from a little below (Alt+Left mouse button down then go up, release the button), you'll see the blue horizontal plane changing to a grey vertical plane. You're now on the vertical mode

Depending of where you are from the maze, it will choose between those two draw planes.

Hints : you can draw in 3D switching between vertical and horizontal mode by moving your point of view of a few degrees. In a next release, the YZ plane will also be available to draw on.

Hints : you can unselect the maze object by clicking outside left the colored plane.

Edit the maze
The green wireframe box is the 3D space where you can edit the maze : it will grow when you draw on the maze and shrink on special actions (remove the last floor).

Let's say you want to create a simple square path to start with. Click with the left mouse button and maintain the button down when drawing on the plane.

I like it when I watch the maze from a top view, so the screenshot will be taken from that point of view.

Then we could go up and draw some more rooms. But before that, maybe you've done some mistakes or want to change something in your maze ? You can remove rooms by two means :

  • change the current mode to : eraser by clicking on the pen button on the scene view (switch between draw and erase modes)
  • Ctrl/Command-Click to remove rooms : the Ctrl/Command modifier works for single click, drags and lines (see below).

To add a floor, you can :

  • rotate the view to switch to vertical mode then draw up : floors are automatically added
  • click on the add floor button on the floors section on the inspector GUI

Let's try going up with the vertical mode, then draw some rooms back in the horizontal mode :

I got to the fifth level ! That's ok, but I just want to go back to the first room now !

Then you can draw a line between those two rooms : just hold Shift while you click on the next room :

  • Select the ground floor on the inspector GUI
  • move your point of view so you're in the horizontal mode
  • Shift+Left click on a room

The tool allows you to draw lines from the last room clicked to the next click : remember that because it could help you a lot !
Here's the created line from the top right orange room to the bottom left cyan room. It's of course generated with a room path, not a straight pixelated line !

Let's try to reduce it a little : remove the last two floors, and connect the red rooms together !

By clicking on the red cross on the right of every floor you can achieve to :

  • remove the floor if it's the last one
  • clear all rooms from the floor otherwise

    The existing links between the rooms are updated so there's no way entering the void. You can check up links by switching the visibility of a floor : click on the eye on the floor line.

You are now ready to use A Maze Kit, congratulations !

For a more evolved usage of A Maze Kit, open the game scene in the CubeShooterDemo or the Sneaker folders, and start from it ! We'll see in the next tutorials what you can do with those themes.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.