Dealing with Obstacles

Introducing Obstacles

After the initialization is complete, you can proceed to processing (registering) obstacles in the game scene.

By processing an obstacle, we mean adding it to the pathfinding graph so that the obstacle is taken into account when searching for a path and the path trajectory does not intersect this obstacle. The navigation graph is built on the basis of an octree containing information about the obstacle.

Each cell of the octree is checked for intersection with at least one triangle from the obstacle mesh. If there is an intersection, then the cell of the graph is marked impassable, otherwise it is passable. Next, based on the free cells of the octree, a passability graph (aka navigation or pathfinding graph) is built.

Obstacles are any game objects (GameObject) on the scene that are active (property gameObject.activeInHierarchy = true) and have a MeshFilter component (on themselves or on members of their Transform hierarchy) with an assigned Mesh field assigned.

There are three types of operations for working with obstacles, all of them are implemented asynchronously:

  • Add: Performs primary obstacle processing. Must be performed for obstacles that have appeared on the game scene after Nav3D initialization, or for those that have not yet been processed. Registers an obstacle or multiple obstacles at pathfinding graph. Without calling this method, pathfinding on the scene will not take into account the obstacle in any way.
  • Remove: Removes an obstacle from the search graph. Must be called for the obstacles you are about to destroy or disable in the scene so that they are no longer taken into account on pathfinding.
  • Update: Updates the changed obstacle in the graph. The method need to be used in case the obstacle was changed after registration, for example, it changed its scale, rotation, or position, or there were changes among the members of the Transform hierarchy (for example, a some child mesh ceased to exist). In fact, it removes the obstacle and then re-adds it to the search graph, but with some optimizations. We do not recommend abusing this method call (it would be a bad idea to call it every Update fiering), since its execution takes a significant amount of time, by analogy with Add operation.