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.

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.

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.

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];
        }
    }
}

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.

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

Switching to the new “Nav Mesh Surface” nav mesh baker fixed this 100% for me.

It’s available at GitHub - Unity-Technologies/NavMeshComponents: High Level API Components for Runtime NavMesh Building

in case if you used terrain trees for designing terrain and afterwards if u need to bake the navmesh,
set scale of the original prefab asset to zero at all axis which you used to plant trees or grass,then rebake.
,
in case if you used terrain trees for designing terrain and afterwards if u need to bake the navmesh,
set scale of the original prefab asset to zero at all axis which you used to plant trees or grass,then rebake.

Yeah so you can basically erase all your trees, hit ‘Bake’ and then just do the Ctrl+Z a few times. Ctrl+Z undoes the trees deletion, but does not affect the navmesh that you baked.

You may can open “navigation” window and set the tree not walkable. Clear old bake, and select "
Bake" option, then bake.