Search Unity

Question How to see if a new obstacle will make path unsolvable?

Discussion in 'Navigation' started by macsimilian, Mar 11, 2024.

  1. macsimilian

    macsimilian

    Joined:
    Sep 19, 2020
    Posts:
    25
    I am making a tower defense game. The player can place towers. Waves of enemies spawn at a certain point and then navigate to the main castle the player is defending. The enemies can't go through the towers and must walk around. I want it so that a player can't place towers such that the enemies have no path; if placing a tower will fully block the path, they won't be able to place it, and a message "would obstruct path" would appear.

    How can I implement this? My first guess would be something like this, but I'm not sure if it would work. First, place the tower as a NavMeshObstacle. Then, try to calculate a path from the enemy spawn point (+ all existing enemies) to the goal castle. Then, check if a path exists (using this). If a path doesn't exist, destroy the newly created tower on the same frame and display the obstruct message. I am not sure if this approach would work, as it would rely on the NavMeshObstacle to carve the NavMesh on the same frame (which I am not sure if it does), so is there a better approach?