Hi. I add a capsule collider and a script to my tree prefab, and import it as a Terrain tree , but, while the capsule collider works, the tree won’t respond to an OnMouseDown. Are scripts not attachable to terrain trees?
Odd. What is your code ?
var objName = “arbor palma”;
function OnMouseDown () {
if (objName == “arbor palma”) print(“The tree received a mouseDown”);
}
The prefab instantiated as a separate object works fine in the scene but not as a Terrain tree as part of the terrain.
I don’t think the tree can be made to receive the OnMouseDown event. However, you may be able to detect the click using a raycast from the camera (see this thread for an example).
Thanks Andeeee. I already do raycast for objects in the scene and I see everything with a collider except those Terrain trees with capsule colliders from my prefab. Terrain trees don’t respond to user-instigated raycasts either, it seems.
The terrain system treats them “special”, they are not objects in your scene like the other stuff.
It is easy to get the tree that corresponds to a particular x/y/z coordinate (actually, x/y). The raycasting will be tricky, however. It depends on your specific setup, if you have a fixed view, you could easily calculate it from the point on the terrain that you click, as the terrain does receive raycasts.
Thanks for confirming that .Tom.
Oh. Great. How do you exactly? Terrain.terrainData…?
Not so easily surely since, unless you click the exact base point of a tree, any click ON a tree will pass through to an x,y point on the terrain behind it, or miss the terrain altogether. Unless that’s wrong?