NavMeshAgent and Closed Doors

My game is a top/down 2D game. I have a Actor (NavmeshAgent) that moves inside a house with about 5 rooms with doors that can be closed or open at different times.

What I would like is that when I ask the Actor to go into a room with a locked door, that he will aknowledge he cannot get there and not even move.

Right now what happens is when i ask the Actor to go into the room he will walk towards the door and since the door has collision he will just stay walking into the door until I send him somewhere else.

I tried to put each door on a different NavMeshLayer and change the status of the NavMeshAgent.walkableMask but since I have several doors that can be open or closed at different times I cant figure out how to change the walkableMask for each possible combination.

Also is this the best way to achieve that or is there another technique?
I’m just using 5 doors but ideally I would have a lot more in the environment, more doors than NavMeshLayers…

I was wondering if there is a way to calculate the path to the room and if that path goes through the door collider than it would become invalid?

I’m afraid Unity doesn’t have a built-in solution for this scenario (unlike Xaitment, who surprisingly solve this exact problem)

You can use raycasts along with the NavMesh to implement spatial awareness for your agents, and implement custom behaviour to stop the agent from running into the door, but calculating an alternative route may prove problematic.

An example of how to use raycasts is to cast a ray between every corner in the path and see if it hits anything.

Another way would be to use the agent’s “eyes” as the source of the raycast.

Worst case scenario you can strap a rigidbody on your agent and stop them from animating at OnCollisionEnter()

As I said, calculating an alternate path can prove a bit difficult, but not impossible. Without a solution such as Xaitment you’ll have to rely on your own implementation (Unity4 does not solve the issue, even with the new NavMeshObstacle component)