Hello Everyone,
I’ve been trying this for a few days, but seems I’m unable to undestand some concepts or I’m missing something on terrain trees, so I need the help from my fellow forum colleagues.
Let’s say I have a terrain, and in this terrain, I generated several trees. I activate the Tree Collider option in the terrain, and added a capsule collider to the prefab. When putting a Character Controller in my camera, it does collide with the tree and I can’t walk through the tree.
Now I want to click on my screen and select a tree, so I thought, seems a good use for raycast, then I did it, but all I get is a reference to the Terrain object. I read somewhere that Unity bakes all my tree colliders with the Terrain collider, and that’s why I’m getting only references to the terrain. Then I thought, okay, I need to check the position where the ray crossed the terrain, and verify if there’s a tree by serching the TreeInstances object. The problem is, doing that is VERY imprecise. If I just compare with the position in the ray, I must click too exactly and this makes me click like hell to select a tree, and if I put some x and z tolerance, I can end selecting several trees at once, and that’s not desired.
Is there a way I can achieve that (clicking and getting the tree I tried to click) correctly?
Thanks in advance.
zharramadar: It may help if you tell us "why" you are doing this. Is this in edit time? Or at run time? And why do you need to find out which tree you've selected?
– Adam-BucknerI am doing this for a RTS game I'm doing. I want to populate the world with trees, and I want to be able to select a particular tree and send a troop to it so it can chop it down and start gathering wood. When clicking a tree with a gatherer selected, it should go to the tree and chop it down (which will remove the treeinstance and place a tree mesh in the place so it can fall down). For that reason, I need to pick one and just one tree. And it is at runtime.
– zharramadarOh, btw, I found a demo from Tom's (which later made the Tom Terrain Tools) where you walk and instance explosions, and then trees fall down, and he's using kinda of what I did so far, but it doesn't count for accuracy, as he's destroying an entire area, and I can do it so far for an area, but I need to pinpoint trees).
– zharramadarSure. The topic I found it: http://forum.unity3d.com/threads/28855-Destroyable-Trees The demo itself: http://web.lemuria.org/~tom/unity/BlastingTrees.html
– zharramadar