Creating and updating NavMesh data at runtime
With the advent of procedural-generated content, more specifically levels, it becomes necessary to come up with techniques that enable us to adjust the navigation areas. This is also important when we have very dynamic levels that can be destroyed in real time in a non-scripted way.
Getting ready
It is important to have the NavMeshComponents
directory in our project as shown in the previous recipe, Setting up our project.
Also, it is important to attach the NavMeshSurface
component to all the parent objects we want to build the new navigation mesh with.
How to do it...
We will create a new component called NavMeshBuilder
:
- Create a new file called
NavMeshBuilder.cs
:
using UnityEngine; using UnityEngine.AI; using System.Collections; using System.Collections.Generic; public class NavMeshBuilder : MonoBehaviour { // Next steps }
- Add a member variable for storing the navigation surfaces:
public NavMeshSurface[] surfaces;
- Implement the method for building...