How it works...

We leverage the power of the NavMesh components to create a smaller component that helps us in building a custom NavMesh around the object.

We take the size of the box around our agent, and the required difference between the box around the previous frame and the current one. If that difference is greater than the percentage we declared in sizeChange, the NavMeshBuilder takes the new position, disposes of the previous navigation mesh, and creates a new one.

That way, we can have an unknown topology, but can navigate it with the same type of agent. Furthermore, it takes considerably less time because we're using the size variable (a Vector3) to create the bounds.

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.