Home > Mobile >  Unity Custom NavMesh obstacle for terrain trees?
Unity Custom NavMesh obstacle for terrain trees?

Time:06-27

I have a terrain with trees on it that is baked into the NavMesh. The NavMesh automatically creates a barrier around the trees, but it seems to be based around the model, not the collider. Since I only want the AI to dodge the trunk of the tree, not the branches, how can I create a custom NavMesh obstacle for the trees? I tried adding a NavMesh obstacle to the tree, but it didn't do anything. Here's a screenshot showing what I mean:

screenshot

Basically, that's too big of a barrier, I want it to be smaller and be able to set a custom NavMesh obstacle on it.

CodePudding user response:

I am quite sure there was option to change navmesh build source from mesh to collider but I cannot find it, I suppose it has been removed from inspector but you can still generate your navmesh from script like that:

Bounds bounds = new Bounds(Vector3.zero, new Vector3(200,20,200);
List<NavMeshBuildSource> sources = new List<NavMeshBuildSource>();        
NavMeshBuilder.CollectSources( bounds, LayerMask.GetMask("Default"),NavMeshCollectGeometry.PhysicsColliders,0, new List<NavMeshBuildMarkup>(), sources );
NavMeshData data = NavMeshBuilder.BuildNavMeshData( NavMesh.GetSettingsByID( 0 ), sources, bounds, Vector3.zero, Quaternion.identity );        
NavMesh.RemoveAllNavMeshData();
NavMesh.AddNavMeshData( data );

CodePudding user response:

If you want to smaller barrier you can change your agent size and re-bake. Here is the agent size settings

And if you want modify the obstacles, you can use navmesh modifier and you can assign as walkable area so navmesh will be ignore this obstacle when baking. https://docs.unity3d.com/2020.1/Documentation/Manual/class-NavMeshModifier.html

  • Related