- Home /
Terrain Trees with Custom Script
I have a tree prefab with a script attached to it. The tree prefab has a capsule collider attached to it and set properly around the tree. I have a character object with a collider attached to it as well. Here is my issue:
I am using the OnCollisionEnter. I placed the tree manually into the terrain and play the game. The result was that the OnCollisionEnter was executed, just as I wanted. I then added the tree using the terrain tool and painted a few of these trees onto the terrain. I was able to collide with the trees, however, the OnCollisionEnter event was not fired. Is there a reason why this event does not fire? or am I simply doing something incorrect?
Answer by Hoeloe · Sep 28, 2013 at 09:16 AM
There is a checkbox on the terrain script that says: "Generate Tree Colliders". It is a really badly labelled box, as it should really say: "Enable Tree Colliders". Unless that box is checked, colliders will be omitted from any trees you place.
Yes I do know this. As stated, the collider's do occur when I paint the trees on the terrain. However, the collisionevent does not fire. I have a debug.log output in the event and it works as a single tree, dragged and dropped on terrain. When I paint the trees using the terrain tools and I collide with the tree I do not get an output. That is my issue.
Ah, I see. I think the issue then is probably that when colliders are attached to trees in the terrain engine, they become parented to the terrain. This means that the colliders are treated as part of the TerrainCollider. In turn, this means that if you character is touching the terrain, and then touches a tree, OnCollisionEnter will not fire, as it is already inside the TerrainCollider. I seem to remember hitting the same "gotcha" myself. You can verify this with a raycast into a tree, and printing off the collider object in a Debug.Log statement.