Hello,
I’m making a grid graph for my terrain. It’s a 2048x2048 units terrain; the grid is 512x512 nodes, with 4 unit-sized square nodes.
At some point, I spawn trees dynamically onto the terrain. I obviously want to update the grid graph every time a new tree spawns to make tree-occupied nodes unwalkable.
And that’s where my problem is: the update is just too slow. I’ve tried using the Dynamic Grid Obstacle script on the trees with different settings, and I’ve also tried manually updating by calling Apply on the included Graph Update Scene script with limited, very small area bounds, and only when a new tree is spawned.
The complete, initial grid scan process for the whole 2048x2048 terrain takes about 1200ms; updating for a single tree, which is about 4x4 and changes at most 2 or 3 nodes, takes about 120ms with any of these methods.
I can only imagine the problem getting even worse when I add moving humanoids and animals and I want to update the graph as they move around so that they don’t walk into one another.
What approach do you recommend for doing these “frequent” updates?
I’ve also tried creating several smaller grids. Same settings, same node size, just divided in four 512x512 grids instead of a big 2048x2048 one. Obviously updates are much quicker on these, but correct placement and alignment is a real hassle.
Hi
Likely the thing that is slow is not the update itself, but something called ‘flood filling’ which is used to calculate which areas of the graph are connected to each other and which are disconnected (this corresponds to the colors of the nodes that you see in the scene view when ‘Graph Coloring’ is set to ‘Areas’) . That information is used to make sure path requests from one part of the world to another part of the world that it cannot reach will fail quickly or find the path to the nearest reachable node instead of having to search through the whole graph (slow) just to figure that out.
If you know that your graph is pretty much completely connected, or you feel like the tradeoff is better, you can disable flood filling. Simply put
return;
At the top of the AstarPath.FloodFill method (if you are using 3.8.2 or lower) or in the GraphUpdateProcessor.FloodFill() method if you are using 3.8.3 or higher.
Doing this will likely improve the performance of those updates quite a bit.
1 Like
Hello Aron,
It worked! Now I can even make my grid graph more detailed that I originally anticipated.
Absolutely amazing support on your part. Thank you very much for your quick, detailed and accurate reply. I will recommend your work to anyone who listens.
Hi
Great that it worked!
If you have the pro version and bought it from the Unity Asset Store, a rating there or a review goes a long way