public void GenerateMaze(int x, int y) { foreach (Transform t in mazeParent) { Destroy(t.gameObject); } maze = new int[SIZE, SIZE]; // true = road; false = wall Carve(x, y, maze); while (true) { endX = Random.Range(0, SIZE); endY = Random.Range(0, SIZE); if (endX != x && endY != y) { treasure.transform.position = new Vector3(endX * 2, 1, endY * 2); break; } } mazeWalls = new GameObject[SIZE * 2, SIZE *2, 2]; for (int i = -1; i < SIZE * 2; i++) { for (int j = -1; j < SIZE * 2; j++) { if (i == -1 || j == -1) { Instantiate(wall, new Vector3(i, 1, j), Quaternion.identity, mazeParent); Instantiate(wall, new Vector3(i, 2, j), Quaternion.identity, mazeParent); } else { mazeWalls[i, j, 0] = Instantiate(wall, new Vector3(i, 1, j), Quaternion.identity, mazeParent); mazeWalls[i, j, 1] = Instantiate(wall, new Vector3(i, 2, j), Quaternion.identity, mazeParent); } } } for (int mapX = 0; mapX < SIZE; mapX++) { for (int mapY = 0; mapY < SIZE; mapY++) { int actualX = mapX * 2; int actualY = mapY * 2; Destroy(mazeWalls[actualX, actualY, 0]); Destroy(mazeWalls[actualX, actualY, 1]); foreach (int d in directions) { int coordX = 0, coordY = 0; switch (d) { case N: coordX = actualX; coordY = actualY + 1; break; case E: coordX = actualX + 1; coordY = actualY; break; case S: coordX = actualX; coordY = actualY - 1; break; case W: coordX = actualX - 1; coordY = actualY; break; } if ((maze[mapX, mapY] & d) > 0) { Destroy(mazeWalls[coordX, coordY, 0]); Destroy(mazeWalls[coordX, coordY, 1]); } } } } surface.BuildNavMesh(); agent.RefreshSetup(treasure, x, y); }