AI in a procedurally generated game

Hi, I’m working on a project that generates very basic square rooms in runtime that are static and don’t contain any moving platforms. The rooms are generated in a tile based manner, and I would like my AI to be able to walk on these tiles. I would like to integrate A* Pathfinding into my project, but I’m unsure as to how I would accomplish this.

I have a prefab for my tiles.

I understand that there are several graphs I can use to achieve this.

Here’s an example screenshot of something generated during runtime.

Hi

Simply generating your procedural level and then calling

AstarPath.active.Scan();

after the level generation is done will work.
You can use either a grid graph or a recast graph. I would probably recommend a grid graph in this case as they are faster to scan.

Hi Aron,
Thanks for your reply!

What does AstarPath.active.Scan() do? Does it automatically create my grid graph for me?
How should I set things up?
Should I just attach a grid graph to an empty game object?

Hi

You can create a grid graph in the inspector outside of play mode (check the example scenes). Just make sure it covers your entire world.
Calling Scan will recalculate the graph to take into account the new rooms that you just created (it will not resize the graph however).

Hi Aron,
Thank you so much!
This works as expected.
Here’s a screenshot of the graph.

However, I have another concern:

In the above image, although rooms are walkable, the opening between rooms is not.
How do I fix the above to ensure the entirety of my rooms including connections between rooms are walkable?

Thank you so much for your help.

Never mind. I fixed the above by decreasing ray length of my grid graph to around 3.
Thank you again!

https://gyazo.com/0ed89ee760e6f1d6f1242a23212fee78

1 Like

Nice that you found a solution.

In case other people stumble across this. The issue was that the arches above the doorways also had colliders, so when the grid graph’s ray length was high enough it would find those arches and place nodes on top of them instead of on the ground below. Reducing the ray length meant that it would always place the nodes on the floor.

An alternative solution would be to put the collider at the of the arch in a separate layer and exclude that from the grid graph by making sure none of the ‘Mask’ fields included that layer.