# Navmesh Obstacles, querying for destination still reachable in TD game

I’m doing sort of a tower defense game and I need to check if all spawn points for enemy units are able to reach the end/target point each time I put a new tower on the map, so towers cannot completely block enemy paths to their final target, I’m using the old unity’s navmesh obstacle to carve the navmesh on runtime.

if only I could ask if after adding a tower (obstacle) and therefore carving the mesh, the mesh is now divided in more sections than before, then I can ask if the section/region/connected polygons that contains the end point and the start point are the same, asking for a path with CalculatePath(spawnPoint, TargetPoint); is not an option because I also need to check if I’m not isolating any of the current enemies in an island:

RED POINT = target/end
GREEN POINT = start/spawn point
CYAN POINTS = enemy units
YELLOW BLOCKS = towers/navmeshObstacles

doing a CalculatePath() for each enemy would be expensive (100+ enemies in the map), so I could just take all islands and do something like this (pseudo code):

``````bool IsTowerCreationForbidden()
{
bool forbidTowerCreation = false;
foreach (Island in Navmesh.Islands)
{
if (island.containsAgent())
{
bool pathFound = island.GetAnyAgentInThisIsland().CalculatePath(start,end);
if (!pathFound)
{
forbidTowerCreation = true;
break;
}
}
}
return forbidTowerCreation;
}
``````

EDIT:

I’ve found out that calling CalculatePath() for lots of agents is not as inefficient as I tought, so while the original question is valid and I would like to solve it, I have a new question:
how can I update the navmesh in the same frame I instantiate the obstacle, to query for paths in the same frame, because otherwise I have to wait an entire frame to get the navmesh carved and then ask for paths to know if there are problems, unity docs says:

Note: When using NavMesh query
methods, you should take into account
that there is a one-frame delay
between changing a Nav Mesh Obstacle
and the effect that change has on the
NavMesh.

in the navmesh obstacle docs

is there any way, maybe with the recent changes in navmesh system in Unity 5.6.0, to manually update the navmesh right after the obstacle is added to the scene, and then calculatePath(), all in the same frame? thanks in advance.

I have a similar problem. I don’t think there is any method to force a NavMesh update, but there should be. It is really inconvenient.

What I ended up doing was turning the code into a co-routine. I then sample the NavMesh position where I know they cutout will (or wont) be yielding for the frame until I get the result I am looking for.

I am only able to do this because my game is turn based. In a real time game this method may not work depending on your requirements.