[Solved] How can I find if there is a gap/s between transforms array?

Jhon RA Asks: How can I find if there is a gap/s between transforms array?

Code:
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 that the distance between two cubes is more than 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

Code:
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

Code:
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 () {
        
    }
}

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.


Maze image example

I tried this to fill the gaps between the bricks of the grid but it’s not working.

Code:
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;


                    }
                }
            }
        }

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.