Hey I have been having trouble with adding tree colliders in my map. I have the “Create tree colliders” option checked, and have the “is triggered” box unchecked. I have no idea how to fix it, thank you.
6 Answers
6I know this topic is old, but it’s also unanswered and I struggled for a few hours tonight, trying to solve the same problem. Am I assuming correctly that you were trying to add trees to an existent terrain after its creation?
The problem is that the Collider doesn’t update automatically. After adding the trees, I did this manually by doing the following:
terrain.GetComponent-TerrainCollider-().active = false;
terrain.GetComponent-TerrainCollider-().active = true;
(replace - with pointed brackets, can’t write this here)
And now it works properly. There might be a better way to do this, but this is what works for me so far.
Why not format the code? terrain.GetComponent<TerrainCollider>().active = false; terrain.GetComponent<TerrainCollider>().active = true; You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window. * http://answers.unity3d.com/page/newuser.html * http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
– AlucardJayAh alright, thank you. I tried to format it properly but for some reason I couldn't get it to work last night. :)
– anon48979042Another easy way:
1.Select the prefab from where you have selected the tree.
2.keeping the prefab selected in asset browser, in inspector, add component mesh collider or box collider or capsule collider and it’s done…
I’ve had this problem. All you need to do is add a prefab for the tree, and then add a capsule collider for the trunk of the tree. This way the “leaves” can still be walked through, but the trunks will prevent you from walking through.
Heres the process:
-
Find the tree you want in the Project panel.
-
Drag it out as a prefab onto your scene anywhere
-
Select the tree, then add: [Components menu → under Physics, → capsule collider]
-
Set the radius of the collier to match the trunk (for me it was about 0.7 on the radius)
-
Make a new prefab, by dragging this “capsule collider’d” tree back to the original
-
use this new tree in the terrain Painter.
Place a few trees, and you should be ready to go! Can’t walk through the trunks now!
If the tree has any other large branches you don’t want to be able to walk through follow the same process and add additional colliders on them.
Its easy!
It's not necessary to create new prefab, just press "apply" button at the top of object inspector
– dimmduh@dimmduh thank you for the update. My answer is 2-3 years old now, and there are probably better ways now.
– the_SimianI have had the same problem before…What I did that ended up working was I put down the prefab of the “tree” I was using, then I put a box collider on it and positioned it corectly around the tree. Then I saved it as a diffent prefab and used it as the tree for the terrain. It worked fairly well.
Is there not a way to get the mesh collider? When I try to add the mesh collider, the console simply says ‘TerrainCollider: MeshCollider is not supported on terrain at the moment.’ I am not sure what to do, and I have tried to use all solutions I have found, not only within this chat but I have gone across the internet, and it seems the only lead I have to the solution I want exists here. I am going to assume that the tree that I am using is not good, and I am simply getting a new free tree asset. The one that I was using before was the ‘Realistic Tree 9 [Rainbow Tree]’
But to no avail, I cannot seem to get or find any solution to this issue. I mean, I am able to add some regular colliders, but I would like to be able to get to the real thing as possible and try to get the mesh collider in for the prefabs. I understand that the way people are solving this issue are to add some capsule collider and to simple change the dimension to that of the trunk of the tree, but let’s be real, this solution is very janky. I do not like this and would love to be able to find a solution where the mesh collider works for the tree.
I wrote a script that iterates over all treeinstances added to a Terrain , for each one I instantiate a new prefab , move it to the original tree location, rotation and scale, then copy just the MeshCollider component and destroy the prefab instance, this way I can keep the terrain trees in tact while including meshcolliders in the scene.
The script creates a copy of the mesh collider for each tree instance and moves the new collider to the source object location adding a new meshcollider in the scene. This allows you to keep the Terrain painted trees while add the meshcolliders in the scene:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(TerrainReader))]
public class TerrainReaderEditor : Editor
{
public override void OnInspectorGUI() {
TerrainReader mainscript = (TerrainReader) target;
if (GUILayout.Button("Place Trees")) //generates real tree objects from the Terrain treeinstances
{
mainscript.PlaceTrees();
}
DrawDefaultInspector();
}
}
#endif
[RequireComponent(typeof(Terrain))]
public class TerrainReader : MonoBehaviour
{
//check out my game: www.mocapfusion.com
public int prototypeIndex = 0; //the terrain tree prefab to use
public Transform container; //Optional , the transform the tree objects will be added to
/// <summary>
/// Generates a "real" copy of the Terrain painted trees, be sure to set the prototypeIndex to the desired tree object if there are multiple tree prototypes.
/// </summary>
public void PlaceTrees()
{
if (container == null) container = transform; //parent new trees to a optional container, else parents new trees directly to the terrain object
Terrain terrain = GetComponent<Terrain>(); //the Terrain component on this gameobject
var trees = terrain.terrainData.treeInstances;
for (int i = 0; i < trees.Length; i++)
{
var tree = trees[i];
if (tree.prototypeIndex == prototypeIndex)
{
var newTree = Instantiate(terrain.terrainData.treePrototypes[prototypeIndex].prefab.gameObject, container);
newTree.transform.position = Vector3.Scale(tree.position, new Vector3(terrain.terrainData.size.x, terrain.terrainData.size.y, terrain.terrainData.size.z)) + terrain.transform.position;
newTree.transform.rotation = Quaternion.Euler(0, tree.rotation * Mathf.Rad2Deg, 0);
newTree.transform.localScale = new Vector3(tree.widthScale,tree.heightScale, tree.widthScale);
CreateColliderOnlyObject(newTree.gameObject);
}
}
}
/// <summary>
/// Creates a copy of the mesh collider, moves collider to source object location then destroys the source object leaving only a collider in the scene.
/// </summary>
void CreateColliderOnlyObject(GameObject sourceObject)
{
var sourceCollider = sourceObject.GetComponentInChildren<MeshCollider>();
var newGo = new GameObject(sourceObject.name);
newGo.transform.parent = sourceObject.transform.parent;
newGo.transform.position = sourceObject.transform.position;
newGo.transform.rotation = sourceObject.transform.rotation;
newGo.transform.localScale = sourceObject.transform.localScale;
var newCollider = newGo.AddComponent<MeshCollider>();
newCollider.sharedMesh = sourceCollider.sharedMesh;
newCollider.convex = sourceCollider.convex;
newCollider.material = sourceCollider.material;
DestroyImmediate(sourceObject);
}
}
Add the script as a component on the gameobject directly next to the Terrain , then set the desired tree prototype index and click “Add Trees” and it generates real tree objects from the Terrain treeinstances.
Now I can use the Terrain system to paint trees, rocks whatever then generate the meshcolliders in the scene so my player can walk on the rocks!! The collisions are perfectly aligned with the painted trees.
Note! Don’t use full res meshes for the meshcolliders in your prefab, I used the LOD3 mesh so the colliders are less complex since I have very many. But it didn’t affect performance or load times and the player does not clip the rocks! Everything appears to be working!
And if you ever change the terrain or paint more trees you can simply delete all the objects in the container and click the button to regenerate the new scene colliders!!
Cheers ![]()

Did you check the layer of the trees and the layer collision matrix?
– Kryptoshow do you check theese colliders?
– ScroodgeMAny luck with these fixes? Hows it coming?
– the_Simian