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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Baking a procedurally generated floor

Discussion in 'Navigation' started by SirDJCat, Aug 18, 2022.

  1. SirDJCat

    SirDJCat

    Joined:
    Jan 4, 2020
    Posts:
    30
    So I'm working on a project with a procedurally generated maze, and need a way to bake a navmesh on the floor. I have a navmesh surface on the floor prefab but it just bakes a square and won't connect to the others. I tried following this video (
    ) but still couldn't figure it out. Any help would be greatly appreciated!
    2022-08-18 (8).png
     
  2. SirDJCat

    SirDJCat

    Joined:
    Jan 4, 2020
    Posts:
    30
    Anyone? :)
     
  3. klgimbel

    klgimbel

    Joined:
    Jan 8, 2015
    Posts:
    1
    You can build you navmesh at runtime, and while there are several ways to go about this, the one that scales the best is if you keep track of all the things that should contribute to navmesh, you can update the navmesh by creating a list of sources as you're loading stuff in. Here's a crude example you can try to experiment with:

    Code (CSharp):
    1. var sources = new List<NavMeshBuildSource>();
    2. foreach (var item in meshRenderers)
    3. {
    4.     sources.Add(new NavMeshBuildSource
    5.         {
    6.             area = 0,
    7.             shape = NavMeshBuildSourceShape.Mesh,
    8.             transform = item.transform.localToWorldMatrix,
    9.             sourceObject = item.GetComponent<MeshFilter>().mesh
    10.         });
    11. }
    12. var bounds = new Bounds(Vector3.zero, new Vector3(2000, 500, 2000));
    13. var agentId = 1;
    14. NavMeshData d = new(agentId);
    15. NavMesh.AddNavMeshData(d);
    16. var settings = NavMesh.GetSettingsByID(agentId);
    17. NavMeshBuilder.UpdateNavMeshDataAsync(d, settings, sources, bounds);
    Now, the NavMeshData you only create once (per agent type you wish to support) but you call NavMeshBuilder.UpdateNavMeshDataAsync everytime you create a new NavMeshBuildSource that would change your navmesh.