Search Unity

Resolved Nav Agent stuck behind door.

Discussion in 'Navigation' started by babaliaris, Jan 12, 2021.

  1. babaliaris

    babaliaris

    Joined:
    Apr 21, 2013
    Posts:
    40
    Check this video to see what is going on:



    In general, I have some trouble making my nav agent smart. Currently, in order to open a door, my logic says "If the agent distance between him and the door < 4 && he's looking at the door, then open the door."

    If the agent tries to open the door from the inside, then this logic works perfectly. He just pushes the door. But in this case, where the agent tries to open the door from the opposite direction (he must pull the door), sometimes he gets stuck.

    I could make the agent move back a little bit before opening the door, but this is something I want to avoid. I want my agent to immediately open the door.

    I could also make the door open if the distance between the agent and the door is longer, so the door won't hit and block the agent, but most of the times because of the way that the nav system works and controls the rotation of the agent, the agent might not rotate to look directly the door and the door will not open.

    Does anyone have a solution for this? Where can I learn more about how to make nav agents smarter?
    Thank you!
     
  2. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    It depends on how you want your game to look. Older games solved this issue by having doors that opened two ways so that the door opened away from the entity and avoided physics/collision issues. If there was an entity on the other side while trying to open the door, it would trigger an event to "push" the entity back, or simply prevent the action from occurring at all.

    Immah dig into the topic more code-wise after I'm done working, but I'd recommend looking into the different states of movement an object can be in, and set it for that the ParentObject for Doors have the inability to collide with any objects until the door is "Open."
    In other words, you can make the agent opening the door and the door itself lose collision as the agent "opens" the door towards himself.
    It's not pretty, but it will give you the building blocks (see: Alpha Build) to refine AGENT movement later on down the line.

    SERIOUS NOTE: When your Agent opens the door, the Door should be returned to it's regular collision state so that other Agents/Player can open/close it as well. Otherwise you'll basically have one door opened that, even when closed, then NavMesh will treat it as if there was no obstacle there and the Agent will noclip through the door.
     
    babaliaris likes this.
  3. babaliaris

    babaliaris

    Joined:
    Apr 21, 2013
    Posts:
    40
    I also thought about making the door a two-way directional opening.

    It would be awesome if the NavAgent script somehow automatically calculated all the NavMeshObstacles that are in the way of the path, and let you query them through code!!! Has anybody before requested that feature?

    In this way you could do something like:
    Code (CSharp):
    1. //Inside Update()
    2. GameObject obstacle = agent.path.GetNextObstacle();
    3.  
    4. if (obstacle .CompareTag("door") )
    5. {
    6.     CustomScript door = obstacle .GetComponent<CustomScript >();
    7.    
    8.     if (door.isTimeToOpen)
    9.        door.Open();
    10.  
    11. }
    Or it could even return a list with all the obstacles that are in the path's way:
    Code (CSharp):
    1. agent.path.GetObstacles();
     
    Last edited: Jan 12, 2021
  4. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    NavMesh should already do that. It's a matter of scripting the behavior of the Agent as it approaches/interacts with the door.

    Remember: Coding is exactly the same as teaching a moron to do a task. Every step should be accounted for. Doors and mirrors are nightmares to code because it requires the actual conceptualization of the task.

    Obviously the Agent is walking to the door, the door is opening, and the Agent is continuing it's path obstructed.
    There's ways to play with NavMesh, but if you want to you could go as far as having the agent approach the door, reach a radius of a door, then have it initiate a method for Door Opening where it redirects the agent to move in front of a door, open the door, and then continue it's route from the new position.

    My idea of removing door collision would help you in this scenario for Alpha/Debugging and is an easy fix, but I have to hit on one very important topic: YOU MUST REINSTATE COLLISION once the door has reached a state of Open or Close, or you'll have issues down the line with all forts of mechanics (ie Weapons/Bullets/OtherAgents moving through doors, Vision going through doors, etc).
     
    babaliaris likes this.
  5. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    Oh. It may be a matter of preference or your implementation, but the "two way door" idea might come off as lackluster AND it still wouldn't solve other Collison and navigation issues you'll encounter dealing with other Agent NavMesh issues.

    I'm being serious about the "teaching a moron" analogy. If you can write down or draw what is going wrong it can help you identify the issue and conceptualize the solution.

    I still use hard-copy notes - but to each, their own.
     
  6. babaliaris

    babaliaris

    Joined:
    Apr 21, 2013
    Posts:
    40
    Actually, I found a better and awesome way! You put an invisible mesh (render only shadows) in the "bad side of the door" where the agent gets stuck, mark it as navigation static, and after placing all the doors and baked your navmesh, then the agent won't be able to walk to these bad sides and get stuck!

    Thankfully this "bad side of the door" is always the same (it's behind the place where the door is located when it is fully opened), so you can put this invisible mesh directly to the door prefab.

    This is how the nav mesh looks like after it is baked:
     
  7. MahaloAloha

    MahaloAloha

    Joined:
    Jan 9, 2021
    Posts:
    31
    Creative solution.

    There's more than one way to skin a cat, after all!

    Just be aware that any instances where the Agent would need to use that location and NOT open a door would present you a new array of issues : but I don't know your intent or goals for this project.
     
    babaliaris likes this.