Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Updating NavMesh Makes it Flat

Discussion in 'Navigation' started by aaronmichaelfrost, Nov 7, 2021.

  1. aaronmichaelfrost

    aaronmichaelfrost

    Joined:
    Feb 4, 2021
    Posts:
    39
    Hello,

    After updating my NavMesh a certain number of times, it becomes flat.

    Original



    After updating:




    Here is the coroutine I start whenever I load a new tile / chunk in my game:

    Code (CSharp):
    1.         private IEnumerator RefreshNavMeshCoroutine()
    2.         {
    3.             if (!navMeshInitiailzed)
    4.             {
    5.                 meshData = new NavMeshData();
    6.                 dataInstance = NavMesh.AddNavMeshData(meshData);
    7.                 navMeshInitiailzed = true;
    8.             }
    9.             sources.Clear();
    10.  
    11.             MeshCollider[] meshColliders = FindObjectsOfType<MeshCollider>();
    12.             for (int i = 0; i < meshColliders.Length; i++)
    13.             {
    14.                 if (!meshColliders[i])
    15.                 {
    16.                     continue;
    17.                 }
    18.                
    19.                 // Mask colliders to include in bake
    20.                 if(TileGeneration.singleton.navMeshLayerMask.value !=  (TileGeneration.singleton.navMeshLayerMask.value | 1 << meshColliders[i].gameObject.layer))
    21.                 {
    22.                     continue;
    23.                 }
    24.  
    25.                 var m = meshColliders[i].sharedMesh;
    26.                 if (!m) continue;
    27.                
    28.                 var s = new NavMeshBuildSource
    29.                 {
    30.                     shape = NavMeshBuildSourceShape.Mesh,
    31.                     sourceObject = m,
    32.                     transform = meshColliders[i].transform.localToWorldMatrix
    33.                 };
    34.                 sources.Add(s);
    35.             }
    36.            
    37.  
    38.             NavMeshData md = new NavMeshData();
    39.  
    40.             NavMeshBuildSettings bs = NavMesh.GetSettingsByIndex(0);
    41.             bs.voxelSize = voxelSize;
    42.             bs.agentRadius = agentRadius;
    43.             bs.tileSize = navMeshTileSize;
    44.             bs.overrideVoxelSize = true;
    45.             bs.overrideTileSize = true;
    46.             bs.agentHeight = agentHeight;
    47.  
    48.  
    49.             var task = UnityEngine.AI.NavMeshBuilder.UpdateNavMeshDataAsync(md, bs, sources, new Bounds(Vector3.zero, Vector3.one * float.PositiveInfinity));
    50.  
    51.  
    52.             while (!task.isDone)
    53.             {
    54.                 yield return null;
    55.             }
    56.            
    57.             dataInstance.Remove();
    58.             NavMesh.RemoveAllNavMeshData();
    59.             dataInstance = new NavMeshDataInstance();
    60.             dataInstance = NavMesh.AddNavMeshData(md);
    61.  
    62.             meshData = md;
    63.             yield return null;
    64.         }
    Please help!

    Thanks.
     
  2. aaronmichaelfrost

    aaronmichaelfrost

    Joined:
    Feb 4, 2021
    Posts:
    39
    I fixed it by reducing the bounds size from infinity to 10000.