Creating a grid at runtime

Im so sorry if this has been covered before but I couldn’t find it in either of the forums…

What i’m looking to do is create a grid at runtime. The level in my game is being generated on start and I don’t know its dimensions until then. I would like to take the dimensions of the level after it has been generated and then generate a grid. I don’t really want to use the Unity editor I’d like to do it all in code if possible?

Thanks for any help

You can modify the graph properties of the a* gameobject with the pathfinding script on it. This script holds all of your graphs and such.

The general approach i would do in script is:

  1. At start i would find the a* script and make a reference to it
  2. At Start() i would find dimensions of the level (using either gameobjects as markers and distances between them or use mesh.bounds if you use a ground plane)
  3. At Start() again, i would then add a new graph
  4. Still in Start() i would set up the graph with dimensions and values you want
  5. Still in Start() then i would called the grid scan method (i cannot remember the name right now in script)

Make sure your game obstacles and floor are in the correct layer or the scan will not complete the way you want.

Also make sure to call repath() on your agents that use this graph to get a path to a target (you might need to specify this new graph you created at runtime)

I believe i have the orderings right. Let us know if this doesn’t work or i am wrong in this approach.


As 1337GameDev said, just clarifying a few things:
5. The method to call is () (or if you like, myAstarPathObject.Scan () ).

GridGraphs can be created and added using the methods in the AstarData class (see docs).