- Home /
Navmesh - How to calculate path AROUND NavmeshAgent Obstacle Radius?
I might be missing the obvious since this seems a bit strange of a behavior.
Let's say we have 2 agents, Agent A and Agent B, and both have a Obstacle Avoidance radius of 0.5.
Agent B is currently static, and Agent A wants to move towards the TargetPos as illustrated below (blue is the navmesh, the red circles represent the Object Avoidance radius for each agent):
My problem is that Unity always Calculates the shortest route to the TargetPos (yellow path), and because of the Obstacle Avoidance, Agent A ends up stuck in the corner, unable to pass by Agent B.
What I would like instead, is something like the green path in the image, where the calculation takes into account Agent B Obstacle Avoidance cylinder and plans around it. Or, since I've realized the agent avoidance doesn't re-calculate the path but does a local avoidance (at least according to the navmesh debug display), to take the longer route through the left side of Agent B so Agent A doesn't get stuck in the edge. Is there a way to do it? Is it part of Unity's NavMesh system?
NOTE: When Agent B is not in a corner, the obstacle avoidance works, and Agent B goes around him. It's just in this situation, where going around means doing the LONGER distance, that he decides to go through the shortest path that he gets stuck.
NOTE2: It's not feasible to add a NavMesh Obstacle component to Agent B since that would mean that if any other agents wants to calculate a path towards him (or he needs to calculate a path to anywhere) they would return as invalid! NavMesh Obstacle component only makes sense for non-agent objects. (Even Unity gives me a warning when I add the NavMesh Obstacle to a GameObject with a NavMeshAgent)
Answer by Jakob_Unity · Jun 22, 2017 at 07:18 AM
I'd still go for carving if the agent is static for a longer time. You need to toggle between the Agent and the Obstacle components, though, as they cannot meaningfully be active at the same time.
But that solution will create a whole new set of much worse problems... The static agent, when trying to figure out if there is a path to anywhere on the map, will always return false, unless I keep toggling the carving off everytime I want to just calculate a path. Even worse, any other agent (moving or not) that wants to know if they can reach the carved agent, will also always return false since there is never a valid path with the carved mesh. (That is why I assumed the agents have their own system without messing the actual navmesh)
Answer by Kurt5 · Jul 12, 2017 at 08:01 AM
You could bake the navmesh at runtime and put a NavMesh Modifier on Agent B that has a high cost and is only active when Agent B is not moving.
https://docs.unity3d.com/Manual/class-NavMeshModifier.html
You could also try messing around with avoidancePredictionTime for the NavMesh
https://docs.unity3d.com/ScriptReference/AI.NavMesh-avoidancePredictionTime.html
The navmesh modifier doesn't solve the issue if there are more agents and seems pretty expensive performance wise.
The avoidancePredictionTime doesn't make any difference, unfortunately.