Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Baking NavMesh at runtine

Discussion in 'AI & Navigation Previews' started by Loustak, Mar 9, 2017.

  1. Loustak

    Loustak

    Joined:
    Dec 23, 2016
    Posts:
    1
    Hello,
    My map is procedurally generated, and I want to use the navigation system. I'm trying to bake the NavMesh at runtime. But my code isn't working.
    Code (CSharp):
    1. private int planeScale = 5;
    2.     private List<NavMeshBuildSource> sources;
    3.  
    4.     public GameObject prefab;
    5.  
    6.     void Start () {
    7.         Vector3 scale = transform.localScale * planeScale;
    8.         sources = new List<NavMeshBuildSource> ();
    9.  
    10.         for (int i = 0; i < (int) scale.x; i++) {
    11.             for (int j = 0; j < (int) scale.z; j++) {
    12.                 if (Random.value > seuil) {
    13.                     Vector3 pos = new Vector3 (2f * i - scale.x, 1.0f, 2f * j - scale.z);
    14.                     Instantiate (prefab, pos, Quaternion.identity);
    15.                     NavMeshBuildSource src = new NavMeshBuildSource ();
    16.                     src.transform = transform.localToWorldMatrix;
    17.                     src.shape = NavMeshBuildSourceShape.Box;
    18.                     src.size = prefab.transform.localScale;
    19.                     sources.Add (src);
    20.                 }
    21.             }
    22.         }
    23.         NavMeshBuildSettings settings = new NavMeshBuildSettings ();
    24.         string[] res = settings.ValidationReport (GetComponent<MeshCollider>().bounds);
    25.         Debug.Log (res); // Return an empty String array
    26.  
    27.         NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, sources, new Bounds(), Vector3.zero, Quaternion.identity);
    28.         Debug.Log (data); // Isn't null
    29.         NavMeshDataInstance inst = NavMesh.AddNavMeshData (data);
    30.         Debug.Log (inst.valid); // Display true
    31.     }
    If in the editor I first bake the nav mesh manually the previous code doesn't modify the nav mesh.
    But if I don't bake it first, I got this warning :
    Failed to create agent because it is not close enough to the NavMesh
    UnityEngine.AI.NavMesh:AddNavMeshData(NavMeshData)

    I'm missing something?
    Thanks for help.

    EDIT: I'm using Unity 5.6.0b11
     
  2. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    421
    Have you tried calling the Bake() function on the NavMeshSurface component?
     
  3. jaakaappi

    jaakaappi

    Joined:
    Aug 17, 2012
    Posts:
    12
    I can't seem to find such a function, although it is mentioned in the documentation. Has it not been implemented yet?
     
  4. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    421
    If you're using the latest version of the repository, you have to use BuildNavMesh() instead of Bake(). Again, located in the NavMeshSurface component. And just in case you didn't know. Even though they said that new NavMesh features would be included in 5.6, these features aren't actually available without downloading the files from GitHub.