Detecting position of an object above another object to make the object under unwalkable.

Hi, I’m still new at using Unity and have a small snag in my random maze project.
I created a script that generates a random maze and then creates a floor of cells under it.
My next step now is to make the player move trough the maze when clicking, but I wanted to make the movement more cell/tactics based to limit movement range since I tried it with a navmesh and the player could just click to the end with no effort.
To do so, I need to make the floor cells under the randomly generated maze cells be unwalkable so that the path finding that I will implement for the player won’t think it can pass trough the walls.

Thank you for any help.
P.S. Here’s the code that generates the maze and an image of the maze.

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


public class MazeGenerator : GameManager
{
    private int[,] maze;
    public MazeCell cellPrefab;
    private MazeCell[,] cells;
    public GameObject wallGo;
    private GameObject cube;

    static System.Random _random = new System.Random();
    void Start()
    {
        maze = GenerateMaze(mazeHeight, mazeWidth);
        for (int i = 0; i < mazeHeight; i++)
            for (int j = 0; j < mazeWidth; j++)
            {
                if (maze[i, j] == 1)
                {
                    Vector3 pos = new Vector3(i, 1, j);
                    GameObject wall = Instantiate(wallGo, transform) as GameObject;
                    if (wall != null)
                        wall.transform.position = pos;
                }
            }
    }
    private int[,] GenerateMaze(int Height, int Width)
    {
        int[,] maze = new int[Height, Width];
        for (int i = 0; i < Height; i++)
            for (int j = 0; j < Width; j++)
                maze[i, j] = 1;

        System.Random rand = new System.Random();

        int r = rand.Next(Height);
        while (r % 2 == 0)
            r = rand.Next(Height);

        int c = rand.Next(Width);
        while (c % 2 == 0)
            c = rand.Next(Width);

        maze[r, c] = 0;

        MazeDigger(maze, r, c);

        return maze;
    }

    private void MazeDigger(int[,] maze, int r, int c)
    {
        // 1 - North
        // 2 - South
        // 3 - East
        // 4 - West

        int[] directions = new int[] { 1, 2, 3, 4 };
        Shuffle(directions);

        for (int i = 0; i < directions.Length; i++)
        {
            switch (directions*)*

{
case 1:
if (r - 2 <= 0)
continue;
if (maze[r - 2, c] != 0)
{
maze[r - 2, c] = 0;
maze[r - 1, c] = 0;
MazeDigger(maze, r - 2, c);
}
break;
case 2:
if (c + 2 >= mazeWidth - 1)
continue;
if (maze[r, c + 2] != 0)
{
maze[r, c + 2] = 0;
maze[r, c + 1] = 0;
MazeDigger(maze, r, c + 2);
}
break;
case 3:
if (r + 2 >= mazeHeight - 1)
continue;
if (maze[r + 2, c] != 0)
{
maze[r + 2, c] = 0;
maze[r + 1, c] = 0;
MazeDigger(maze, r + 2, c);
}
break;
case 4:
if (c - 2 <= 0)
continue;
if (maze[r, c - 2] != 0)
{
maze[r, c - 2] = 0;
maze[r, c - 1] = 0;
MazeDigger(maze, r, c - 2);
}
break;
}
}
}

public static void Shuffle(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i–)
{
int j = random.Next(i);
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}

public void Generate()
{ cells = new MazeCell[mazeHeight, mazeWidth];
for (int x = 0; x < mazeHeight; x++)
{
for (int z = 0; z < mazeWidth; z++)
{
CreateCell(x, z);
}
}
}

private void CreateCell(int x, int z)
{
MazeCell newCell = Instantiate(cellPrefab) as MazeCell;
cells[x, z] = newCell;
newCell.name = "Maze Cell " + x + ", " + z;
newCell.transform.parent = transform;
newCell.transform.localPosition = new Vector3(x - mazeHeight * 0f, 0f, z - mazeWidth * 0f);

}

}
[115989-maze.png|115989]*
*

Navmesh is completely the wrong way to go about this for a maze game. Basically, what you’re saying is that you want to present a challenge to the player, and then make the main mechanics cut the challenge apart. Sounds rather boring. I think you should try a different mechanism for movement than this. Traditional arrow key movement would work, and there are other options. However, if you are dead-set on using NavMesh for this, here’s one way. Create a 2 dimensional list of Booleans, where each item represents a tile in the maze. Assign the list in the same way you create cells. When each value is true, it means there is a wall in the way. Then, you have to use that in the NavMesh calculations to make him avoid those tiles.

Hope this helps!

The title of this question doesn’t really reflect the problem you’re trying to solve. Your goal here seems to be “let the player click somewhere, let the game automatically move them, but don’t let them move too far.”

Did you check out CalculatePath? A combination of that and determining the distance your player will travel using the path’s corners property should produce the behavior you want. You’d do something like:

// 1- Raycast the player's click / touch onto the board
// 2- Check if there's a path from your player to that position
// 3- If there is, check how long it is using the path's corners
// 4- If that's within your distance tolerance, let the player follow it