Search Unity

Resolved NavMesh created at runtime not recognized by agents or NavRaycast

Discussion in 'Navigation' started by cmd_Unity, Dec 24, 2021.

  1. cmd_Unity

    cmd_Unity

    Joined:
    Apr 21, 2019
    Posts:
    5
    Hi,

    I using meshes gathered from an AR-Device to create a navmesh at runtime.
    I can clearly see the navmesh in the scene at runtime, but neither my navAgents nor a NavRaycast recognize this navmesh.
    Left picture at runtime where the navmesh is not created => the vertical raycast going through the agent is green = no navhit
    Right picture at runtime when the navmesh is created => same, navraycast still green = no navhit (should be blue when navraycast hit navmesh)
    And yes, my agent is aligned properly over the mesh ;-)

    upload_2021-12-24_10-22-45.png




    I am using UInity 2020.3.22f1, here are the relevant code snippets:

    first nav-mesh creation:
    Code (CSharp):
    1.  
    2. //param: parent = GameObject contiaing multiple GameObjects with Meshes
    3. public static void CreateNavMesh(GameObject Parent)
    4. {
    5.    List<NavMeshBuildSource> sources = new List<NavMeshBuildSource>();
    6.  
    7.    MeshCollider[] mcs = Parent.GetComponentsInChildren<MeshCollider>();
    8.    if (mcs == null)
    9.    {
    10.       Debug.LogError("CreateNavMesh: could not find any meshes");
    11.    }
    12.    else
    13.    {
    14.       foreach (MeshCollider meshcollider in mcs)
    15.       {
    16.          sources.Add(CreateNavMesh_MeshBuildSource(meshcollider));
    17.       }
    18.       navMeshBuildSettings.agentRadius = 0.05f;
    19.       navMeshBuildSettings.agentHeight = 0.1f;
    20.       navMeshBuildSettings.agentSlope = 60.0f;
    21.       navMeshBuildSettings.agentClimb = 0.1f;
    22.       navMeshBuildSettings.voxelSize = navMeshBuildSettings.agentRadius / 5f;
    23.       navMeshBuildSettings.minRegionArea = 0.01f;
    24.       NavMesh.RemoveAllNavMeshData();
    25.       NavMeshBuilder.UpdateNavMeshData(navMeshData, navMeshBuildSettings, sources, CalculateLocalBounds_groupedObject(Parent));
    26.       NavMesh.AddNavMeshData(navMeshData);
    27.       UnityEditor.AI.NavMeshBuilder.BuildNavMesh();  //this was only for test, its not neccessary for navmeshcreation
    28.     }
    29. }
    30.    
    31. private static NavMeshBuildSource CreateNavMesh_MeshBuildSource(MeshCollider meshcollider)
    32. {
    33.    var src = new NavMeshBuildSource();
    34.    src.transform = meshcollider.gameObject.transform.localToWorldMatrix;
    35.    src.shape = NavMeshBuildSourceShape.Mesh;
    36.    src.sourceObject = meshcollider.sharedMesh;
    37.    return src;
    38. }
    39.  

    second the navagents update, doing navmeshraycasts

    Code (CSharp):
    1.    
    2. [SIZE=4]void Update ()
    3. {
    4.     navOnMesh = nav.isOnNavMesh;
    5.  
    6.     if (navOnMesh == true)
    7.         CmdMove();
    8.  
    9.     else if (navOnMesh == false)
    10.     {
    11.         NavMeshHit hit;
    12.         Vector3 from = transform.position + new Vector3(0f,100f,0f);
    13.         Vector3 to = transform.position - new Vector3(0f, 100f, 0f);
    14.         bool blocked = NavMesh.Raycast(from , to, out hit, NavMesh.AllAreas);
    15.         if(hit.distance == Mathf.Infinity)
    16.            Debug.DrawLine(transform.position + new Vector3(0f, 100f, 0f), transform.position - new Vector3(0f, 100f, 0f), blocked ? Color.red : Color.green);
    17.         else
    18.            Debug.DrawLine(transform.position + new Vector3(0f, 100f, 0f), hit.position, blocked ? Color.red : Color.blue);
    19.  
    20.  
    21.     }
    22. }[/SIZE]
    23.  

    Has anyone an idea how to teach my agent or navmeshraycast to use my created navMesh?

    Thank you in advance,
    Martin
     
    Last edited: Dec 24, 2021
    Lighter02 likes this.
  2. cmd_Unity

    cmd_Unity

    Joined:
    Apr 21, 2019
    Posts:
    5
    Ok found the solution:

    First:
    I missunderstood NavMeshRayCast which is not intended the way I did used it. I am using now
    NavMesh.SamplePosition() instead.
    => now the Raycast turns blue = samplePosition "hit" NavMesh

    Second:
    I did not recognize that the "availabiltiy" of a NavMesh for Agents depend on the AgentTypeID used to create the NavMesh. So now I added in Line 18 ( where I do define NavMeshBUildSettings):
    Code (CSharp):
    1. navMeshBuildSettings.agentTypeID = -1325456; // the weird AgentTypeID of my Agent
    => now the NavAgent recognizes the NavMesh and walking on it.



    upload_2021-12-24_12-32-23.png


    Hope it helps others too!
     
    Lighter02 likes this.