- Home /
NavMeshAgent and doors?
The help page on navigation mentions the complex environments of games and the obstacles within, such as doors- but stops short of explaining how to manage these circumstances.
Can anyone tell me how I could manage a scenario where a door could be locked or open, and tell a nav mesh agent to to around or through the door depending on its state?
Thanks, -Rich
I'm currently having the exact same problem. I looked at the documentation, the navmesh demos and the $$anonymous$$anual and can't figure it out... should we use the off mesh link? That seems to be for gaps or for certain characters to use certain routes... Any help really appreciated!
I've been playing a bit more and understand how to place certain navmesh sections into different layers and by changing the walkable$$anonymous$$ask for the agent he is able or unable to walk there. But I still cant figure out how to use the Offmesh link for doors...
Where does the door go though? Another option is making the door dynamic navmeshagents that can shut and open. If you don't have too many that would be pretty cheap.
I've read an answer here were someone mentioned that but I don't know how to set it up and can't understand how it would fix it. If a navmeshAgent takes all the path space then the path for the other agent becomes invalid?
Also in my case I can stop the agent from going into the room since the door collision is blocking him when the door is closed but he just keeps walking forwards trying to pass through it. He doesn't really detect that the path is impossible. Would the navmeshAgent solution fix it?
In my case its a top down view kind of game. The player can click directly on the room he wants to go into but if the door is closed the path should be just impossible and he wouldn't move.If its open he can walk there.
Answer by drawcode · Sep 25, 2012 at 09:38 PM
I haven't tested this but you can activate or deactivate an OffMeshLink: http://docs.unity3d.com/Documentation/Components/class-OffMeshLink.html
Your script could find the OffMeshLink and activate or deactivate the link based on the door condition.
Activated Specifies if this link is actually used by the pathfinder. When this property is false, the off-mesh link will be disregarded. This property is runtime-editable, and does not require a re-bake.
Yeah, I tried to use these but I couldn't get any results.. Also in the Unity demos (http://beta.unity3d.com/3.5-preview/Nav$$anonymous$$eshTest-3.5.0f1.zip) they don't seem to use the dynamic offmeshlink that you are suggesting but actually the static one.
Also those off-mesh links, at least looking at the image, won't they make the player jump from pointA to pointB ins$$anonymous$$d of navigating it properly? I'm going to try something this evening and post the code here to see if it helps understanding why I cant get it to work
I added a offmesh link to the surface near the door and activating or de-activating doesnt seem to do a difference for the character, he can always cross it. Also from looking at the documentation it says the off-mesh links are for "It is possible that the navmesh static geometry in the scene is disconnected, thus making it impossible for agents to get from one part of the world to the other"
Which is not the case...I just want the path to a certain location to be blocked temporarly. I think its related to navmesh layers but I cant figure it out... help would be really good
I've moved on to something else at the moment, but as an idea could you create a nav layer that only covers the area of the door? and on the base layer create a gap in the door threshold. Then when the door layer is enabled the layers overlap and create a path. Having a layer for every door doesn't sound ideal if you have many doors though. I have a feeling this system isn't entirely complete
Yeah, I can see how that would work but in my case I would need too many layers...Also the player still tries to get there, he just stops on the boundary between the 2 layers...
I noticed that you can do a pathStatus and you get a "pathPartial" ins$$anonymous$$d of "pathCompleted" if I use your suggestion maybe I can use that to stop the player from even moving.
But still it feels like extremely complicated and I have too many rooms with too many doors to have this work nicely. Also if I use layers I would end up having to change what layer each player can use ins$$anonymous$$d of changin the current navmesh layer and thus making the doors independent from the characters and just more clean overall... I'm sure there must be an answer to this!
Ok, I've resorted to using Rich$$anonymous$$akeGame suggestion and placing each door in a different Nav Layer. But now how do I set the walkable$$anonymous$$ask based on what doors are open or closed? Those are set as bitfields so I have no idea how to do all the possible combinations.
Answer by CouldNotThinkOfOneSorry · Jul 23, 2016 at 07:43 AM
@RichMakeGame Try looking at this:
https://docs.unity3d.com/Manual/nav-AreasAndCosts.html
This link explains NavMesh areas and costs. I have yet to actually try it, but here's my quick/TL;DR understanding:
1) You can select meshes you character will walk on, and specify a NavMesh area.
2) This area can then have it's cost altered. The NavMeshAgent/s that use the NavMesh will automagically prefer routes with lower costs. [=>]
3) This area can also be excluded from a NavMeshAgent's AreaMask. Doing this stops the NavMeshAgent from walking through this area; the example from the manual is a Zombie being unable to open a door, but you could also have a system where you need a key to open a door. [==>]
Hope this helped. Sorry if you figured this out 4 years ago. :\
=> An example of this could be something like trying to traverse a river by bridge vs wading through waist-high water - you'd much rather walk on the bridge, because it's faster (has a lower cost). BUT, if the bridge is far away, and the destination is directly across from you, then wading may be faster (have a lower cost).
==> To do this, you might fire an event that changes the NavMeshAgent's AreaMask ([VariableForNavMeshAgent].areaMask = ???) when your NavMeshAgent touches a mcguffin.
NOTE: Changing Off-Mesh links (enabling/disabling), NavMesh area costs or NavMeshAgent AreaMasks sets the "isPathStale" flag to true (https://docs.unity3d.com/ScriptReference/NavMeshAgent-isPathStale.html).
If the path is stale, it indicates the path may no longer be valid (not sure here, sorry) or optimal (bridge/river example: if you have boots that let you walk on water (it's a game :p), walking the 1km to a bridge, across it, then 1km to a goal is MUCH slower than walking across the 500m river).
Answer by MarkD · Oct 26, 2013 at 05:08 PM
all suggestions are good. what i have done once is to create a mesh (like a plane) in the shape of the floor. then only let it calculate its mesh path on that object. this way you will have a path wich is the same as your floor without doors. another thing you could do is to dissable the doors on the moment of baking and re-enable them after the bake. if it needs to interact place a trigger that detects the ai and let the ai wait and open or close the door/other functions
Answer by rogodoy · Mar 11, 2021 at 01:10 PM
To work with doors and collision in nav mesh, try use "nav mesh obstacle" in the objects you want collinsion, it acts like box collider in nav mesh.
Your answer
Follow this Question
Related Questions
NavMeshAgent Rotate With Model 0 Answers
Get NavMeshAgent to look where it's going 0 Answers
How to create a car ai that wonders around searching for player 0 Answers
how to generate infinite nav mesh path 2 Answers