Unity3d Pathfinding Obstacles To Critical Thinking

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)

Nav Mesh Obstacle

The Nav Mesh Obstacle component allows you to describe moving obstacles that Nav Mesh Agents should avoid while navigating the world (for example, barrels or crates controlled by the physics system). While the obstacle is moving, the Nav Mesh Agents do their best to avoid it. When the obstacle is stationary, it carves a hole in the NavMesh. Nav Mesh Agents then change their paths to steer around it, or find a different route if the obstacle causes the pathway to be completely blocked.

PropertyFunction
ShapeThe shape of the obstacle geometry. Choose whichever one best fits the shape of the object.
Box
        CenterCenter of the box relative to the transform position.
        SizeSize of the box.
Capsule
        CenterCenter of the capsule relative to the transform position.
        RadiusRadius of the capsule.
        HeightHeight of the capsule.
CarveWhen the Carve checkbox is ticked, the Nav Mesh Obstacle creates a hole in the NavMesh.
    Move ThresholdUnity treats the Nav Mesh Obstacle as moving when it has moved more than the distance set by the Move Threshold. Use this property to set the threshold distance for updating a moving carved hole.
    Time To StationaryThe time (in seconds) to wait until the obstacle is treated as stationary.
    Carve Only StationaryWhen enabled, the obstacle is carved only when it is stationary. See Logic for moving Nav Mesh Obstacles, below, to learn more.

Details

Nav Mesh Obstacles can affect the Nav Mesh Agent’s navigation during the game in two ways:

Obstructing

When Carve is not enabled, the default behavior of the Nav Mesh Obstacle is similar to that of a Collider. Nav Mesh Agents try to avoid collisions with the Nav Mesh Obstacle, and when close, they collide with the Nav Mesh Obstacle. Obstacle avoidance behaviour is very basic, and has a short radius. As such, the Nav Mesh Agent might not be able to find its way around in an environment cluttered with Nav Mesh Obstacles. This mode is best used in cases where the obstacle is constantly moving (for example, a vehicle or player character).

Carving

When Carve is enabled, the obstacle carves a hole in the NavMesh when stationary. When moving, the obstacle is an obstruction. When a hole is carved into the NavMesh, the pathfinder is able to navigate the Nav Mesh Agent around locations cluttered with obstacles, or find another route if the current path gets blocked by an obstacle. It’s good practice to turn on carving for Nav Mesh Obstacles that generally block navigation but can be moved by the player or other game events like explosions (for example, crates or barrels).

Logic for moving Nav Mesh Obstacles

Unity treats the Nav Mesh Obstacle as moving when it has moved more than the distance set by the Carve > Move Threshold. When the Nav Mesh Obstacle moves, the carved hole also moves. However, to reduce CPU overhead, the hole is only recalculated when necessary. The result of this calculation is available in the next frame update. The recalculation logic has two options:

  • Only carve when the Nav Mesh Obstacle is stationary

  • Carve when the Nav Mesh Obstacle has moved

Only carve when the Nav Mesh Obstacle is stationary

This is the default behavior. To enable it, tick the Nav Mesh Obstacle component’s Carve Only Stationary checkbox. In this mode, when the Nav Mesh Obstacle moves, the carved hole is removed. When the Nav Mesh Obstacle has stopped moving and has been stationary for more than the time set by Carving Time To Stationary, it is treated as stationary and the carved hole is updated again. While the Nav Mesh Obstacle is moving, the Nav Mesh Agents avoid it using collision avoidance, but don’t plan paths around it.

Carve Only Stationery is generally the best choice in terms of performance, and is a good match when the GameObject associated with the Nav Mesh Obstacle is controlled by physics.

Carve when the Nav Mesh Obstacle has moved

To enable this mode, untick the Nav Mesh Obstacle component’s Carve Only Stationary checkbox. When this is unticked, the carved hole is updated when the obstacle has moved more than the distance set by Carving Move Threshold. This mode is useful for large, slowly moving obstacles (for example, a tank that is being avoided by infantry).

Note: When using NavMesh query methods, you should take into account that there is a one-frame delay between changing a Nav Mesh Obstacle and the effect that change has on the NavMesh.

See also

  1. Creating a Nav Mesh Obstacle - Guidance on creating Nav Mesh Obstacles.

  2. Inner Workings of the Navigation System - Learn more about how Nav Mesh Obstacles are used as part of navigation.

  3. Nav Mesh Obstacle scripting reference - Full description of the Nav Mesh Obstacle scripting API.

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *