- Home /
Can I ignore trees when baking NavMesh on Terrain?
I understand that in Unity 3.5, the NavMesh system was updated to bake the mesh around trees. However, it seems as though there is no option to ignore them!
For the purposes of my current project, I actually want to ignore trees for the purposes of hard navigation via NavMeshAgent.
I have tried the following:
Uncheck 'Create Tree Colliders' on Terrain Collider component and re-bake navemesh
Uncheck 'Draw Trees' on Terrain Component Settings tab and re-bake navmesh
Ultimately I realise that I could bake the navmesh before applying the trees, but unfortunately that is a time-consuming process since our artist has already created our terrain.
Another option is to use custom navigation - something we've considered but obviously a simpler solution would be superior!
Any help or advice would be greatly appreciated.
Answer by pipiroo6 · Apr 13, 2014 at 02:52 PM
I had the same problem, my only solution was delete all the trees with the big Brush, Bake the NavMesh, and then undo with Ctrl + z.
In that way you can keep your artist modifications and have the proper nav mesh.
It's far from be elegant, but Im running out of ideas.
And we are here, 2017, and stay the same. Thank you for workaround.
thanks! you saved me! :) you are so creative, this is nice trick
Answer by Dubious-Drewski · May 12, 2014 at 02:00 PM
This is a glaring oversight and a big pain in my butt. Why does the navmesh generator completely ignore the "create tree collider" option? Now my AI carefully avoids stepping on any tiny plants or grass even though none of them have a collider attached. It's ridiculous.
Is this problem submitted in the bugs forum? Having to erase all foliage, then generating navmesh, then ctrl-z ing everything back is cumbersome. And what if you need to have walkable shrubs but you need trees to block paths? It can't be done unless you add shrubs at the very last step of level creation. This is a SERIOUSLY ANNOYING limitation.
Almost 10 years later, this is still a very real problem. How did they never fix this?
Answer by unity-marcus · Jul 11, 2020 at 02:55 PM
Fixed it by creating a small script which first erases all trees and then restores them:
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.AI;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TerrainNavMeshBaker
{
private static Dictionary<Terrain, TreeInstance[]> _terrainTrees = new Dictionary<Terrain, TreeInstance[]>();
[MenuItem("Tools/Bake NavMesh")]
public static void BakeNavMesh()
{
try
{
RemoveTrees();
NavMeshBuilder.BuildNavMesh();
}
finally
{
RestoreTrees();
}
}
private static void RestoreTrees()
{
foreach (var terrain in _terrainTrees)
{
terrain.Key.terrainData.treeInstances = terrain.Value;
}
}
private static void RemoveTrees()
{
var terrains = new List<Terrain>();
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
if (!scene.isLoaded)
continue;
terrains.AddRange(scene.GetRootGameObjects().SelectMany(x => x.GetComponentsInChildren<Terrain>()));
}
_terrainTrees.Clear();
foreach (var terrain in terrains)
{
_terrainTrees[terrain] = terrain.terrainData.treeInstances;
terrain.terrainData.treeInstances = new TreeInstance[0];
}
}
}
'TerrainNavMeshBaker' is missing the class attribute 'ExtensionOfNativeClass'!
Answer by CampbellFletcher · Dec 05, 2017 at 08:53 PM
pipiroo6's workaround has inspired me, and i have one that some might prefer: make a prefab of an empty game object, and when you need to bake, hit "edit tree" and choose this prefab for all trees you want to ignore. Still not perfect, but less cumbersome for me at least.,pipiroo6's workaround has inspired me, and i have what might be a better one for some people: make a prefab of an empty game object, and when you need to bake, hit Edit Tree and replace the tree(s) in question with this prefab. then you can put the originals back when you're done baking. still not perfect, but slightly less cumbersome in my opinion.
Answer by Cameon-57 · Dec 31, 2018 at 09:57 PM
I just found a similar answer, and it is based off of the ctrl + Z method. I had already finished my terrain, with large amounts of grass a trees, so naturally didn't want to erase everything. After reading the ctrl + Z idea, I realized that you could delete the trees (or grass) from the terrain brush tool, Re-Bake the navmesh, then press crtl + Z to replace the vegetation. It should not remove any of the vegetation after you press ctrl + Z, but i do advise saving the scene first in case something doesn't go right. I found this to be an faster solution.
I'm using Unity 2018.2
Also, if you want something to be detected by the Navmesh, don't delete it from the terrain brush tool.
Your answer
Follow this Question
Related Questions
Edit navmesh 2 Answers
Marking area on Terrain 1 Answer
Unity 3.5 Pathfinding with terrain and trees. 5 Answers
Reducing Lag due to Large Particle Systems and Trees 1 Answer
Can't lightmap trees! (or Character) 1 Answer