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
I am using UInity 2020.3.22f1, here are the relevant code snippets:
first nav-mesh creation:
//param: parent = GameObject contiaing multiple GameObjects with Meshes
public static void CreateNavMesh(GameObject Parent)
{
List<NavMeshBuildSource> sources = new List<NavMeshBuildSource>();
MeshCollider[] mcs = Parent.GetComponentsInChildren<MeshCollider>();
if (mcs == null)
{
Debug.LogError("CreateNavMesh: could not find any meshes");
}
else
{
foreach (MeshCollider meshcollider in mcs)
{
sources.Add(CreateNavMesh_MeshBuildSource(meshcollider));
}
navMeshBuildSettings.agentRadius = 0.05f;
navMeshBuildSettings.agentHeight = 0.1f;
navMeshBuildSettings.agentSlope = 60.0f;
navMeshBuildSettings.agentClimb = 0.1f;
navMeshBuildSettings.voxelSize = navMeshBuildSettings.agentRadius / 5f;
navMeshBuildSettings.minRegionArea = 0.01f;
NavMesh.RemoveAllNavMeshData();
NavMeshBuilder.UpdateNavMeshData(navMeshData, navMeshBuildSettings, sources, CalculateLocalBounds_groupedObject(Parent));
NavMesh.AddNavMeshData(navMeshData);
UnityEditor.AI.NavMeshBuilder.BuildNavMesh(); //this was only for test, its not neccessary for navmeshcreation
}
}
private static NavMeshBuildSource CreateNavMesh_MeshBuildSource(MeshCollider meshcollider)
{
var src = new NavMeshBuildSource();
src.transform = meshcollider.gameObject.transform.localToWorldMatrix;
src.shape = NavMeshBuildSourceShape.Mesh;
src.sourceObject = meshcollider.sharedMesh;
return src;
}
second the navagents update, doing navmeshraycasts
[SIZE=4]void Update ()
{
navOnMesh = nav.isOnNavMesh;
if (navOnMesh == true)
CmdMove();
else if (navOnMesh == false)
{
NavMeshHit hit;
Vector3 from = transform.position + new Vector3(0f,100f,0f);
Vector3 to = transform.position - new Vector3(0f, 100f, 0f);
bool blocked = NavMesh.Raycast(from , to, out hit, NavMesh.AllAreas);
if(hit.distance == Mathf.Infinity)
Debug.DrawLine(transform.position + new Vector3(0f, 100f, 0f), transform.position - new Vector3(0f, 100f, 0f), blocked ? Color.red : Color.green);
else
Debug.DrawLine(transform.position + new Vector3(0f, 100f, 0f), hit.position, blocked ? Color.red : Color.blue);
}
}[/SIZE]
Has anyone an idea how to teach my agent or navmeshraycast to use my created navMesh?
Thank you in advance,
Martin