Maze Game Drawing

Hey everyone! I wanted to do a simple maze game, but I’m running into an issue with drawing the main result.

I know there isn’t a problem with the actual mazes - I did this code originally in C++ for a class project. My problem is properly drawing the walls. My idea was to use as follows:

public GameObject[] rWalls;
public GameObject[] dWalls;
public GameObject rWall;
public GameObject dWall;
public Transform rLayout;
public Transform dLayout;

I only need to store the right and down walls for each cell, and doing so allows me to avoid “doubling up” on walls. dWalls and rWalls are arrays of GameObjects instantiated as dWall and rWall respectively. dWall and rWall are pretty much Sprite Prefabs - they’re dummy objects housing only a single Sprite component, which is just a simple thin black line representing a horizontal or vertical wall. Their parent transforms are set to dLayout and rLayout respectively, which are supposed to space out the walls of the maze. My idea was to draw everything by default, then check my actual maze object to see if a wall exists in the corresponding spot. If it doesn’t, I deactivate the respective element of dWalls or rWalls.

My end result is a vertical wall and a horizontal wall in the middle. I’m assuming that every drawn wall is just aggregating in the same, central spot. How can I properly space everything out? And what’s the best way to deal with moving the objects? I was too used to treating the top-left corner as the 0/origin - what’s the smoothest way to shift everything to the proper location from the center of the screen?

Thanks in advance!

Question… back when you did this project for school…did you actually get this program working with C++ code, did you finish it? Did it work and display a maze on the screen?

You haven’t actually put up the code that’s not working… Where do you actually call Instantiate?

Right, unfortunately Unity can’t guess where you want stuff to appear; you have to actually tell it.

Assign to the .transform.position of each one after you instantiate it. Or, you can actually do this as part of the instantiation — see the first example in the reference manual.

using UnityEngine;

public class InstantiateExample : MonoBehaviour
{
    public GameObject prefab;

    void Start() {
        for (int i = 0; i < 10; i++) {
            Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
        }
    }
}
2 Likes

I have some old maze code lying around here if you want to take a look:

http://www.coolfone.mobi/mazer5/maze.h

I used the code in a 3d shooter where you destroy robots.

It uses depth-first search.

Here’s some interesting information on how to use depth-first search in maze generation:

https://en.wikipedia.org/wiki/Maze_generation_algorithm#Depth-first_search

I would think if you are using Unity, you may want to build a mesh that contains the maze.

Also, there is maze stuff in the Unity Asset store such as this:

https://www.assetstore.unity3d.com/en/#!/content/38689

There is even a web demo of that one:

http://styanton.github.io/MazeGenerationDemo/

Have a great day.

Yes, but the display method was different. We used a PNG class (or, a slightly modified version) and directly modified the pixels via pointers. The end result was a static image. No wall objects or anything like that.

For another project I used a Transform Layout to make grids of button/toggle prefabs with no issue. I’m just confused as to why assigning my objects to follow the layout didn’t move the walls objects this time. Should I have assigned the layout to the sprite component instead of the prefab object itself?

Anyway, here’s what I’ve got going on now:

public Maze maze;
public GameObject[] rWalls;
public GameObject[] dWalls;
public GameObject rWall;
public GameObject dWall;

void Start () {
        maze = new Maze(Screen.width/20, Screen.height/20);
        dWalls = new GameObject[(Screen.height / 20 + 1) * (Screen.width / 20 + 1)];
        for(int i = 0; i <= maze.w; i++)
        {
            for(int j = 0; j <= maze.h; j++)
            {
                dWalls[maze.IntPairToInt(i, j)] = (GameObject)Instantiate(dWall, new Vector3(i*20, j*20, 0), Quaternion.identity);
            }
        }
        rWalls = new GameObject[(Screen.width / 20 + 1) * (Screen.height / 20 + 1)];
        for(int i = 0; i <= maze.w; i++)
        {
            for(int j = 0; j <= maze.h; j++)
            {
                rWalls[maze.IntPairToInt(i, j)] = (GameObject)Instantiate(rWall, new Vector3(i * 20, j * 20, 0), Quaternion.identity);
            } 
        }
        DrawMaze();
    }

public void DrawMaze()
    {
        dWalls[0].SetActive(false); //disable the entry wall
        for(int i = 1; i <= maze.w; i++)
        {
           for(int j = 1; j <= maze.h; j++)
            {
                if (!maze.walls[i-1][j-1].first) rWalls[maze.IntPairToInt(i, j)].SetActive(false);
                if (!maze.walls[i-1][j-1].second) dWalls[maze.IntPairToInt(i, j)].SetActive(false);
            }
        }
    }

(IntPairToInt helps me map between a 2D and 1D representation of my maze).

But all I get is a single, vertical wall in the center.

Well, for what it’s worth, you can do exactly the same sort of thing in Unity using the PixelSurface asset. (Or by writing your own code to write to & manage textures.)

I’m not sure why your code here isn’t working, though. You’re assigning different positions in your Instantiate call. I suspect there’s something else (some other script) that is resetting the positions of these things back to the origin. Or else, it’s something goofy like your camera view is so large, that even having these things a few hundred units apart, they look like they’re all in the same place.

While your game is running, select one and look at its position in the Inspector. Is it really at 0,0,0? And, is there some other script on them that could be responsible for that?

1 Like

I’m getting confused as well…

When I run it, I get a bunch of the Prefabs listed in the hierarchy. The first row of right and down walls (indicating the top and left walls of the maze) are all active minus the entry wall, and the rest of the prefabs are activated/deactivated in a way that I’d say looks “mazelike enough.” They have proper/different coordinates… and yet only a single wall is being drawn. Is there something wrong with the way I set up my prefabs?

This is a pretty simple project so far - the MazeGen script is the only script besides the supplementary objects. And it’s the only script that touches or includes the maze wall objects. I’ve shown you all of the code that touches the walls… so it must be an external issue?

It sounds like your walls are off-camera except for one.

I would switch from Game view to Scene view and try zooming out.

Remember, Instantiate is working with world coordinates, not screen coordinates.

2675854--188944--Screen Shot 2016-06-12 at 8.42.33 PM.jpg

Also, you may be able to get those off-camera walls to show by changing the camera from Perspective to Orthographic and increasing the size to something like 300:

2675854--188945--Screen Shot 2016-06-12 at 8.53.16 PM copy.png

2 Likes