# Fill in the small empty gaps (Maze Generator)

So I created a generator that is able to generate different mazes using random seeds. Now the problem I am running into is how to fill in the tiny gaps that it creates when doing different maps. I want to leave the bigger area for a player to move around.

Code:

public float cellSize = 1;
public int XSize = 20;
public int ZSize = 20;

[Range(0,100)]
public int randomFillPercent;

public GameObject Cell;

public string seed;
public bool useRandomSeed;

public Transform MazeHolder;
private GameObject[,] Maze;

private void Start()
{
GenerateMaze();
}

private void GenerateMaze()
{
Maze = new GameObject[XSize, ZSize];
CreateMaze();
}

private void CreateMaze()
{
if (useRandomSeed)
{
seed = Random.Range(0,999999999).ToString("0");
}

System.Random pseudoRandom = new System.Random(seed.GetHashCode());

for (int x = 0; x < XSize; x++)
{
for (int z = 0; z < ZSize; z++)
{
if (x == 0 || x == XSize - 1 || z == 0 || z == ZSize - 1)
{
Maze[x, z] = (GameObject)SpawnCell(x, z);
}
else if (pseudoRandom.Next(0, 100) < randomFillPercent)
{
Maze[x, z] = (GameObject)SpawnCell(x, z);
}
}
}
}

private GameObject SpawnCell(int X, int Z)
{
float newX = cellSize * X;
float newZ = cellSize * Z;

GameObject newCell = Instantiate(Cell, new Vector3(newX, 0, newZ), Quaternion.identity);

newCell.transform.SetParent(MazeHolder);

return newCell;
}

An image of the “tiny spaces” would help confirm, but I suspect this issue is due to the fact that the Mesh you use for each cell is NOT precisely cellSize, in world space. From the description I’d guess it is slightly smaller that cellSize, or maybe slightly offset from model-space center.

You could use the [bounds] dox link member of MeshRenderer (assuming the cell has one) e.g. cell.GetComponent<MeshRenderer>().bounds to get the actual, rendered size of the cell, in worldspace. You can then use this value, instead of the hard-coded ‘20’.