# How can I find if there is a gap/s between transforms array?

for (int i = 0; i < cubes.Length; i++)
{
if(bricks[i].transform.position.z == 1)
{
var distance = Vector3.Distance(bricks[i].transform.position, )
}
}


I want to find all the places where the distance between two cubes is > 0. For example if there is a gap between index 6 and index 7, then give me the gap position it's empty space, but give me this position where there was should be a cube.

Each couple of cubes check if there is a gap between them and return the gap position.

I have a maze randomly generated and there are some entrances I want to find the entrances by checking for gaps each gap position is entrance.

This is the maze create class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Maze
{
//Grid size
public int width;
public int height;

//Store grid
private bool[,] grid;
//Generate random directions to move
private System.Random rg;

//Start position
public int startX;
public int startY;

//Public getter
public bool[,] Grid
{
get { return grid; }
}

//Constructor of the grid for setting values
public Maze(int width, int height, System.Random rg)
{
this.width = width;
this.height = height;

this.rg = rg;
}

//Generate the grid
public void Generate()
{
grid = new bool[width, height];

startX = 1;
startY = 1;

grid[startX, startY] = true;

MazeDigger(startX, startY);
}

void MazeDigger(int x, int y)
{
int[] directions = new int[] { 1, 2, 3, 4 };

//We create random array of directions
HelpingTools.Shuffle(directions, rg);

//We are looping over all the directions
for (int i = 0; i < directions.Length; i++)
{
if (directions[i] == 1)
{
if (y - 2 <= 0)
continue;

if (grid[x, y - 2] == false)
{
grid[x, y - 2] = true;
grid[x, y - 1] = true;

MazeDigger(x, y - 2);
}
}

if (directions[i] == 2)
{
if (x - 2 <= 0)
continue;

if (grid[x - 2, y] == false)
{
grid[x - 2, y] = true;
grid[x - 1, y] = true;

MazeDigger(x - 2, y);
}
}

if (directions[i] == 3)
{
if (x + 2 >= width - 1)
continue;

if (grid[x + 2, y] == false)
{
grid[x + 2, y] = true;
grid[x + 1, y] = true;

MazeDigger(x + 2, y);
}
}

if (directions[i] == 4)
{
if (y + 2 >= height - 1)
continue;

if (grid[x, y + 2] == false)
{
grid[x, y + 2] = true;
grid[x, y + 1] = true;

MazeDigger(x, y + 2);
}
}
}
}
}


And this is the maze generate script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MazeGenerator : MonoBehaviour
{
public Maze maze;
public int mazeWidth;
public int mazeHeight;
public string mazeSeed;
public GameObject wallPrefab;

private GameObject wall;
private GameObject wallCorner;
private System.Random mazeRG;

// Use this for initialization
void Start ()
{
mazeRG = new System.Random();

if (mazeWidth % 2 == 0)
//mazeWidth++;

if (mazeHeight % 2 == 0)
{
//mazeHeight++;
}

maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
}

public void GenerateMaze()
{
maze.Generate();
DrawMaze();
}

void DrawMaze()
{
for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == true)
{
CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);
}
}
}

var bricks = GameObject.FindGameObjectsWithTag("MazeBrick");

bricks[bricks.Length - 1].GetComponent<Renderer>().material.color = Color.red;

//bricks[Random.Range(0, bricks.Length - 1)].GetComponent<Renderer>().material.color = Color.red;

for (int i = 0; i < bricks.Length; i++)
{
if (bricks[i].transform.position.x == maze.startX &&
bricks[i].transform.position.z == maze.startY)
{
bricks[i].GetComponent<Renderer>().material.color = Color.red;
}

if(bricks[i].transform.position.z == 1)
{
var distance = Vector3.Distance(bricks[i].transform.position, )
}
}
}

void CreateMaze(Vector3 position, Transform parent, int sortingOrder, float rotation)
{
GameObject mazePrefab = Instantiate(wallPrefab, position, Quaternion.identity);
mazePrefab.transform.SetParent(parent);
mazePrefab.transform.Rotate(0, 0, rotation);
mazePrefab.tag = "MazeBrick";
}

// Update is called once per frame
void Update () {

}
}


And the HelpingTools script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelpingTools : MonoBehaviour
{
public static T[] Shuffle<T>(T[] array, System.Random rg)
{
for (int i = 0; i < array.Length - 1; i++)
{
int randomIndex = rg.Next(i, array.Length);

T tempItem = array[randomIndex];

array[randomIndex] = array[i];
array[i] = tempItem;
}

return array;
}
}


The maze is generate randomly. with random entrances/exits depending on what you are define as entrances or exits but for this example lets say they are all entrances.

I positioned the main camera in one of the entrances but I want that after the maze has generated to position the camera automatic randomly on one of the entrances.

Later on I want the camera to enter the selected entrance and travel through the maze. but for now first I want to find all the entrances and position the camera in front of one of them. I tried this to fill the gaps between the bricks of the grid but it's not working.

for (int i = 0; i < bricks.Length; i++)
{
var brick = bricks[i];

for (int x = 0; x < bricks.Length; x++)
{
if (brick.transform.position.z == 1)
{
var distance = Vector3.Distance(brick.transform.position, bricks[x].transform.position);
if (distance > brick.transform.localScale.x)
{
continue;
}
if(distance == 1f)
{
// josh.position.x + (mark.position.x - josh.position.x) / 2;

var block = Instantiate(wallPrefab, new Vector3(bricks[x].transform.position.x + 1,0,brick.transform.position.z), wallPrefab.transform.rotation);
block.GetComponent<Renderer>().material.color = Color.green;
}
}
}
}


If I'm trying this loop :

for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == false)
{
var t = Instantiate(wallPrefab, position, Quaternion.identity);
t.GetComponent<Renderer>().material.color = Color.yellow;
}
}
}


Then it will fill all gaps in yellow and also will fill the surrounding of the maze. How do I fill only the gaps of the outer of the maze ? This is is not working either :

for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
if (x == mazeWidth - 1 || y == mazeHeight - 1)
{

Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == false)
{
var t = Instantiate(wallPrefab, position, Quaternion.identity);
t.GetComponent<Renderer>().material.color = Color.yellow;
}
}
}
}


Updating with my last testing :

for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == false)
{
if (x == mazeWidth - 1 || x == 1 || y == mazeHeight - 1 || y == 1)
{
var t = Instantiate(wallPrefab, position, Quaternion.identity);
t.GetComponent<Renderer>().material.color = Color.yellow;
}
}
}
}


but still the result is not good enough.

And another update, I'm almost there.

I still don't get it yet if the maze size is 30 on 30 why when I click on bricks that on the outer of the maze the Z position is 27 and not 30 ?

And why some bricks are out of the outer of the maze ?

for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == false)
{
if (x == 27 || x == 1f || y == 27 || y == 1f)
{
var t = Instantiate(wallPrefab, position, Quaternion.identity);
t.GetComponent<Renderer>().material.color = Color.yellow;
}
}
}
}


The result is : In this screenshot the top show a brick outside the maze what is this ? X is at 1 but Z is at 29 than on the bottom I clicked on a brick in the outer maze area and it is the z on 27.

So why its coloring some on z 29 ? and why the maze is size 27 on 27 but I set it to be 30 on 30 ?

And maybe the maze is 30 on 30 but all the rows and columns from 30 to 27 are empty gaps ?

I don't understand it yet.

• What did not work? Is your array of cubes called cubes oder bricks? Distance will always return something greater than 0 if their centers are not on top of each other. How are your bricks organized if you have only a single index for them. Aug 2, 2021 at 16:39
• Correct me if I'm wrong, but your grid in your maze class represents your full maze. You can simply iterate through the first and last row/ column of x/y and check if it is true or false to find if there is a wall or not. Aug 3, 2021 at 10:03
• @Zibelas can you show me please how to do it ? I updated my question with what I tried and I can fill the whole maze gaps in yellow but I want on to fill only the gaps in the outer area of the maze not like in the screenshot. Aug 3, 2021 at 17:31
• You have it almost, your loop goes over your full maze, but you just want the outer rows of it. Aug 3, 2021 at 18:41
• @Zibelas can you please correct my last try in my question ? it's close but still not good enough. Aug 3, 2021 at 22:17