Search Unity

Make NavAgent avoid obstacles only partially

Discussion in 'Navigation' started by Tom163, Oct 19, 2021.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    The scenario:

    Player can drop obstacles into path of AI enemies. The obstacles aren't insurmountable, so crossing them (climbing or destroying them) is possible. But it takes time. If there's one block on an empty plane, it's faster to just walk around it. If there are many or if it blocks off a corridor, it's faster to climb/break it.

    I'm thinking of various ways to implement this, and then I thought "somebody probably solved this already".

    I could run the path calculation with and without those obstacles and compare the path distance. Or maybe there is a way to change the navmesh area underneath the obstacle into a different area with a higher cost?

    What is the best way to solve this scenario?

    (Note that in my case, obstacles are created by player at a relatively low rate (about one per second at most, and not at all most of the time) so if the required operations are expensive, that's ok.
     
    KqGMRPFkAeESzF8 likes this.
  2. KqGMRPFkAeESzF8

    KqGMRPFkAeESzF8

    Joined:
    Nov 20, 2018
    Posts:
    53
    I have the same problem, been stuck on it for days. I created a crappy workaround by disabling all navmesh object components if the enemies' path corners length is above a certain value so that they won't go too far out of their way if the player creates a maze for them to traverse that is very long, and will instead just go straight towards the player and destroy any obstacles in their path.

    Another slightly less crappy but still crappy workaround is to duplicate your navmesh surface, move it below the original one by a certain distance, for me I added 100 to the z position because I'm working in 2D. Then create an empty child gameobject for the enemy prefab and then copy paste the navmesh agent component onto that empty child so that each enemy has two agents attached to them. Offset the second agent down the same amount as the duplicated navmesh surface. Then make sure your navmesh obstacle gameobjects don't intersect with the second navmesh surface. Whenever you want a certain enemy to ignore the obstacles, disable the first navmesh agent, and enable the second one which is attached to the navmesh surface that doesn't intersect with any navmesh obstacles. This will at least allow you to calculate the difference between the path lengths for with and without having obstacles in the way.
     
    Last edited: Oct 25, 2021
  3. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
  4. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I couldn't get NavMesh Modifier Volumes to work, so I'm now switching to A* Pathfinding instead of the built-in NavMesh.