- Home /
Not allow instantiate obstacle that make destination unreachable
Hi all !
In the case of a tower-defense game using navmesh (with tiled floor), is it possible to detect if the construction of a tower will make the destination unreachable ?
I tried to instantiate an invisible obstacle on the tile on which my mouse was over and print "blocked" if the path become partial and it work but, the navmeshagent goes wierd during that... Is there another way that I didn't found ?
Thanks in advance !
Someone correct me if I'm wrong, but I think Unity's native navmesh pathfinding system is ill-suited to such a task. In my investigation some year(s) ago when the feature was introduced, I was given the impression that dynamic obstacles had some substantial limitations. At least, I recall rejecting it for my project at the time because dynamic obstacles were a key feature and I didn't like the way they were handled.
In any case, the only procedure I am aware of for verifying path walkability is a brute-force test. I do this at load time for my generated maps (different project altogether), but I'm using classic a-star, not Unity's navigation system. $$anonymous$$ethinks the principle would be the same, though. Pick every point from which an agent can start, and try to get a path to every point the agent can end. If any of these attempts to get a path fails, you have blocked an essential corridor and created an unwalkable scenario.
What further assistance do you require?
Thanks for your reply !
Sure, it's probably easier to use a proper A* script. But I don't really understand how to use a "brute-force test" on my case... Do you mean that I have to instantiate an invisible obstacle each time my mouse is over a tile, recalculate the path and if it's blocked, destroy that obstacle, declare that tile "unbuildable" and finally recalculate another path for agents to resume on it?
Wouldn't that drop performance a lot ?
"Do you mean that I have to instantiate an invisible obstacle each time my mouse is over a tile, recalculate the path and if it's blocked, destroy that obstacle, declare that tile unbuildable..."
Yes, that is my suggestion. From every entry point, calculate a path to every goal point. If any of these paths cannot be found, it means the obstacle you are previewing would create an "island" in your nav mesh.
"Wouldn't that drop performance a lot?"
It's certainly something you should do as little as possible. Only once per previewed obstacle. If you discover performance is suffering, you could perhaps distribute the work over several frames using a coroutine and yield statements between checks. Simply disallow any decision-making during the check procedure.
Wow ! That worked like a charm ! Thanks a lot for the concept ;)