Search Unity

Runtime navmesh alteration

Discussion in 'Animation' started by MrDude, May 10, 2014.

  1. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Hia

    I have a large area I want AI characters to navigate. In the middle of this is a huge door that is closed at the start of the level but may or may not open during the course of the level.

    Now, when I build my nav mesh setting the doors to navigation static, this results in the enemy chasing me until I get to the door and then just stopping and staring at me before going back on patrol because the navmesh doesn't allow for him to go through the gate...

    On the flip side of the coin, if I don't mark the doors as navigation static then the nav mesh DOES allow for the enemy to pass through the gate but now, even though the gate it closed and the box collider prevents me from going through the closed gate, the enemy can pass through the solid gate like ghosts... They have capsule colliders on them and if I jump and land on one of them I can actually stand on their heads so their colliders are active and not set to be triggers... but when the navigation system tells them to go to point X, they move straight through solid objects to get there...

    Okay, so, now comes the question... how do I stop this?

    I understand the whole point of generating the nav mesh in the first place is for the game to know "This place is reachable and this place is not because there is something in the way. Go around it" I get that... Do does this mean that a navmesh can only be used in scenes where there is absolutely no dynamic content in the scene?

    I.e. if I create the nav mesh and later have a building explode, covering the path with debree and blocking it completely... what if a stubborn ox walks in front of a passage and stops to eat some hay? Or in this case, what if I have a does I want the AI enemy to go through but only if the door is actually open.

    Is the only way to do this by saying "If the path is blocked, don't tell the enemy to go to the other side" or must I recreate the nav mesh at runtime?

    The "do the logic in code so they don't try and go to there" approach won't work cause I have many enemies, all of them all over the place and they can try to approach the player from any direction and from both sides of the gate (since my character can actually climb over walls to unlock the gate from the inside) and once one sees me, he raises the alarm and then all the enemies come after me... even those behind other walls/gates/buildings... adding custom logic to each enemy based on his path and the location of the path and the various gates throughout the level.... it's just not practical... but as far as I am aware, neither is recreating the nav mesh every time I open or close a door...

    So how do I prevent an enemy with a collider from moving through solid objects when controlled by navigation?

    Thanks in advance
     
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    You have several options creating/accessing doors in unity.

    1. Use OffMeshLink(s) to connect across threshold navmesh gaps (when a door opens).
    - you can set the navmeshlayer on each offmeshlink - to control access (navmeshagent needs a compatible walkable mask).
    (see also OffMeshLink)

    2. Use NavMeshObstacle to carve a threshold navmesh gap (when a door closes).
    (see also NavMeshObstacle)

    3. Use NavMeshLayers to mark the navmesh below the door - control the access by modifying the agent(s) walkableMask.
    (see also NavMeshLayers and NavMeshAgent.walkableMask)

    Cheers..
    /Jakob
     
  3. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Thanks for that

    I really like that there seems to be an actual workable plan for this and that I won't have to resort to some kind of underhanded wangling to do this. Great news.

    As it is, I was using the A* package before this and as I understand it, these two systems work basically the same with the exception being how you define the nav mesh area... point is, this thing is not the ultimate mystery to end all mysteries to me but I have never before used OffMeshLinks and the documentation didn't make it quite clear to me why I would or how I would use them... I looked at it a few times but it just took the facts that made sense to me and make me so confused that I wanted to just go back to using the A* package rather than the built in navigation system...

    Now that I seem to have an actual need for it, perhaps now it might make more sense when I read it again... Let's hope.

    Thanks again for the info. OffMeshLinks... would not have thought of that...