- Home /
Getting treePrototype information from RayCasting collisions onto terrain?
I need to be able to look at a tree on a terrain and be able to get what type of tree it is (say, an Oak vs. a Pine). A ray cast from the camera seems to collide with the trees just fine, but only returns "terrain" as the collider touched. I'm not too concerned with the individual tree, merely its type, and the scene itself is static, so the terrain won't change during runtime right now.
In a nutshell, I need the get the specific treePrototype from a ray cast collision on the terrain.
I know the terrain API is murky, but is there a good way to do this? The scene itself has thousands of trees, so looping through every single treeinstance to find the tree I'm looking at isn't really feasible, nor is placing colliders by hand as separate objects.
It seems like I should be able to find out the original treePrototype prefab quickly if I'm raycasting to its capsule collider, but perhaps not - another possibility would be to look at texture data, if the terrain stores the tree data there along with ground info. Any suggestions?
Answer by Dreamer · May 13, 2011 at 03:18 AM
The trees created by paint tool on terrain have no collision. You need to get the tree and add collider to make a prefab, then place them on terrain.
I know 1000 trees is a bit more, but at least Unity can automatically place right upon terrain height.
I've been able to add tree colliders to the prefab just fine (I now know the textures don't have that information), but it still lists a raycast collision as "terrain", and not the tree type. Is there any way to find out what kind of tree the ray collided with? It seems I should be able to get that information without looping through every single tree instance, which would be very slow.
In short, I need the ray to return the tree prefab type, given a collision with the tree collision "capsule".
Are you placing the prefab manually or use terrain paint? If manually, you just set the tag of the tree.
I'm using terrain paint, so any collision simply registers the entire terrain ins$$anonymous$$d of the single tree - and there are far too many trees in the scene to add information manually.
So far as I know, if you use terrain paint, the tree is part of terrain. You only can place prefab manually way to achieve what you want.
Answer by toddf · May 17, 2011 at 05:39 PM
The approach I ended up using was actually creating the collider capsules on my own, using a script (shown by a coder with a similar problem) that looped through each of the treeInstances within terrainData and created a collider that matched the location on the existing terrain. You end up rebuilding some of the terrain information on your own this way, but it's far more accessible. Having a script that does this offline is practically a must, as it takes a few minutes to calculate. Once it's done, however, you'll have capsules you can add metadata to, and whose individual info can easily be read by a ray cast.