I know the title is really vague but I didn’t know how to keep it concise enough. I found this maze generation script for C# and I wanted to implement it in Unity, but I’m having a little trouble with converting the output to instantiated rooms for each cell in the maze.
I have a room prefab with 4 walls, and a Room script which enables/disables each wall (I renamed the cell states to cardinals since it works better for my brain):
public class Room : MonoBehaviour
{
public GameObject[] walls; // 0 - North : 1 - East : 2 - South : 3 - West
public void UpdateWalls(CellState cs)
{
walls[0].SetActive(cs.HasFlag(CellState.North));
walls[1].SetActive(cs.HasFlag(CellState.East));
walls[2].SetActive(cs.HasFlag(CellState.South));
walls[3].SetActive(cs.HasFlag(CellState.West));
}
}
Then I have a quick generator script that implements things:
public class Generator : MonoBehaviour
{
public int mazeWidth;
public int mazeHeight;
public Vector2 roomDims;
public GameObject room;
void Start()
{
var maze = new Maze(mazeWidth, mazeHeight);
for (int x = 0; x < maze.Width; x++)
{
for (int y = 0; y < maze.Height; y++)
{
var pos = new Vector3(x * roomDims.x, 0, y * roomDims.y);
var go = Instantiate(room, pos, Quaternion.identity);
go.GetComponent<Room>().UpdateWalls(maze[x, y]);
}
}
}
}
So I just happened to notice that your bottom row pictured has a north wall in every cell, and the top row has a south wall in every cell. (West and east columns look okay.) Are you sure you have your Y coordinates correct for north and south?
You’re definitely right on that, though I’m not sure why it’s happening. I didn’t change anything major in the original maze generator script. The only reason I can see is that the Display function doesn’t translate as nicely as I’d hoped, and that there needs to be some adjustments. I’m really not certain how to even approach this.
Just switch to anti-clockwise direction starting from south.
public class Room : MonoBehaviour
{
public GameObject[] walls; // 0 - South : 1 - East : 2 - North : 3 - West
public void UpdateWalls(CellState cs)
{
walls[0].SetActive(cs.HasFlag(CellState.South));
walls[1].SetActive(cs.HasFlag(CellState.East));
walls[2].SetActive(cs.HasFlag(CellState.North));
walls[3].SetActive(cs.HasFlag(CellState.West));
}
}
I think this should work, unless something else we don’t know about is happening somewhere.