Search Unity

Check for blocking placements before actually place them (for TD game)

Discussion in 'AI & Navigation Previews' started by Cleverlie, Aug 30, 2017.

  1. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    211
    Hi people, I'm stuck with something critical for I think most TD games, I had to switch to A* pathfinding project asset because I didn't figure out a way to achieve this with Unity's own navmesh system:

    is there any way to do something similar to what A* offers with this method in its API? is for when you are about to place a new tower in the map, you want to check if the tower will completely block the path from units to the target position:

    --------------------------------------------------------------------------------------
    Check for blocking placements
    A common thing in tower defence games is to make sure that the towers the player places does not block the path between the spawn points and the goal. This might seem hard to do, but luckily there is an API for that.

    The Pathfinding.GraphUpdateUtilities.UpdateGraphsNoBlock method can be used to first check if a given graph update object will cause the path between two or more points to become blocked. This is however slower than a normal graph update so you probably don't want to use it too often.

    For example when a player in a tower defence game places a tower, you could instantiate it, then call the UpdateGraphsNoBlock method to check if the newly placed tower will block the path. If it did then remove the tower immediately and notify the player that the choosen position was not valid. You can pass a list of nodes to the UpdateGraphsNoBlock method so you could for example make sure that not only is the path from the start to the goal not blocked, but also that all units can still reach the goal (if it is possible to place towers when enemies are walking around).

    Code (CSharp):
    1. var guo = new GraphUpdateObject (tower.GetComponent<Collider>.bounds);
    2. var spawnPointNode = AstarPath.active.GetNearest (spawnPoint.position).node;
    3. var goalNode = AstarPath.active.GetNearest (goalNode.position).node;
    4. if (GraphUpdateUtilities.UpdateGraphsNoBlock (guo, spawnPointNode, goalNode, false)) {
    5.     // Valid tower position
    6.     // Since the last parameter (which is called "alwaysRevert") in the method call was false
    7.     // The graph is now updated and the game can just continue
    8. } else {
    9.     // Invalid tower position. It blocks the path between the spawn point and the goal
    10.     // The effect on the graph has been reverted
    11.     Destroy (tower);
    12. }
    13.  
    -----------------------------------------------------------------------------------------

    https://arongranberg.com/astar/docs/graph-updates.php#blocking

    if only I could manually trigger a syncronous call to navmesh Update(), then check for reachability somehow, and trigger the update() again or rolling back changes in case of fail, all in the same frame, that will do it.

    Thanks for any answer on this, is the thing that is keeping me from being able to use unity navmesh into my TD game, cheers!
     
unityunity