- Home /
 
Procedural NavMesh Instantiating
Hello everyone,
I'm trying to create a simple procedural map in unity, so I can generate worlds from images. Yet, I do have a problem with creating NavMeshes after I applied the vertices and triangles for the MeshFilter and MeshCollider.
I've tried to find as much as possible about NavMesh generation, but - unfortunately - I concluded that there isn't much diverse content online that could help me solve my problem.
The actual problem

As you can see in the image above, I created a simple world that has a MeshRender, MeshFilter and MeshCollider applied to it. Right in the bottom left corner I created a cube - displayed in orange - that consists of a MeshRender, MeshFilter, MeshCollider, Rigidbody and a NavMeshAgent. The goal is to get the cube 'walk' to another position (on the same height). Yet, I need to have a NavMesh that grants the NavMeshAgent to 'walk'.
This is the code I'm using:
 public class LevelManager : MonoBehaviour
 {
     // ...
 
     // map requirements
     MeshFilter meshFilter;
     MeshRenderer meshRenderer;
     MeshCollider meshCollider;
 
     // nav mesh agent
     public NavMeshAgent agent;
 
     void Awake ()
     {
         // set up the procedural map
         // ...
         
         // set the nav mesh
         NavMeshBaker navMeshBaker = new NavMeshBaker (gameObject, new Vector3 (30f, 2f, 20f));
         navMeshBaker.Bake ();
     }
 
     void Start ()
     {
         // try to move the nav mesh agent
         agent.destination = new Vector3 (30f, 1.5f, 3f);
     }
 }
 
               And here is my NavMeshBaker Class:
 public class NavMeshBaker
 {
     // nav mesh margins
     public GameObject obj;
     public Transform center;
     public Vector3 buildSize;
 
     // nav mesh data (instamce(s))
     private NavMeshData         data;
     private NavMeshDataInstance instance;
 
     // build source
     private List <NavMeshBuildSource> sources;
 
     // constructor
     public NavMeshBaker (GameObject obj, Vector3 buildSize)
     {
         this.obj = obj;
         this.center = obj.transform;
         this.buildSize = buildSize;
 
         sources = new List <NavMeshBuildSource> ();
         sources.Add (GetLevelBuildSource (obj.transform, buildSize));
     }
 
     public NavMeshBuildSource GetLevelBuildSource (Transform trans, Vector3 size)
     {
         NavMeshBuildSource src = new NavMeshBuildSource ();
 
         src.transform   = trans.localToWorldMatrix;
         src.shape       = NavMeshBuildSourceShape.Box;
         src.size        = size;
 
         return src;
     }
 
     public Bounds GetBounds (Transform trans, Vector3 size)
     {
         return new Bounds (trans.position, size);
     }
 
     
     public void Bake ()
     {
         NavMeshBuildSettings buildSettings = NavMesh.GetSettingsByID (0);
 
         Bounds localBounds = GetBounds (center, buildSize);
 
         NavMeshBuilder.BuildNavMeshData (buildSettings, sources, localBounds, Vector3.zero, Quaternion.Euler (Vector3.forward) );
     }
 
     public void Remove ()
     {
         // yet to be implemented
     }
 }
 
 
              Your answer
 
             Follow this Question
Related Questions
When baking a Navmesh Surface at runtime 'Include Layers' property gets ignored 2 Answers
How to know if a navmesh is baked at runtime? 0 Answers
What is the difference between configuration of agent in 'Agents' tab and in 'Bake' tab Unity? 1 Answer
NavMeshAgent Not Accounting for Objects 0 Answers
Navmesh Pedestrian Crossing 1 Answer