hi, im working on a project with randomly generated mazes. to do this i instantiate a prefab “cell” (with a floor and 4 walls ) in a grid (example 10x10) and then use DFS to breakdown the walls between to generate the maze. all of this works fine and i am able to generate random mazes at runtime.
what i want to do next is to add pathfinding to an object and move it around the maze. since i intend the maze to be generated at runtime, and the size of the maze to be dynamic, i want to setup the gridgraph using code. which is where i run into problems.
i found some code to do this on this forum and while i am able to get the grid graph generated, it does not accept the size of the maze i want to use and instead sets the grid.width and grid.depth to 10.
using the inspector at runtime and manually changing the values and hitting scan gives me what looks like a good gridgraph.
the code im using is as follows.
`
void start() {
UnityEngine.Object temp;
for (int y = 1; y <= height; y++) {
for (int x = 1; x <= width; x++) {
Vector3 pos = new Vector3(x, 0, y) * spacing;
temp = Instantiate(prefab, pos, Quaternion.identity);
temp.name = "Cell_" + (((y*width)-width)+x); // More convinient names for each cell instance.
}
}
// DFS CODE HERE
makepath(100); // height * width
}
void makepath(int msize){
GridGraph g = (GridGraph)AstarPath.active.astarData.CreateGraph(typeof(GridGraph));
g.neighbours = NumNeighbours.Four;
g.width = msize;
g.depth = msize;
g.center = transform.position + (Vector3.right * 10f) + (Vector3.forward * 10f);
g.bounds = new Bounds(g.center, new Vector3(16, 0.3f, 16));
Matrix4x4 m = g.matrix;
g.GenerateMatrix();
g.RelocateNodes (m, g.matrix);
AstarPath.active.astarData.AddGraph( g );
GridGraph gridNow = (GridGraph)AstarPath.active.astarData.graphs[0];
//AstarPath.active.UpdateGraphs(g.bounds);
AstarPath.active.Scan();
}
`
being new to both unity and the a* project i dont know where im going wrong. i apologize in advance if this is a simple mistake i am making.
p.s also i would like to know if there is a simpler way to achieve what i want to do.
thank you in advance.