Problem raycasting against painted tree collider

Hello,

I have a tree prefab that I made and atttached a capsule collider to the tree prefab. I then put that tree prefab into my terrain “painter” options, and I painted the trees.

However, when I run the game, even though the physics collisions work on the tree collider between objects, I cannot raycast against the tree collider. What might be the cause of this?

Can you give more information about what you are trying to accomplish? Like…do you want to interact with trees, maybe chop them down? Or are you saying that when you do a raycast…it isn’t anything at all?

When you “paint” a tree on the terrain, its collider actually becomes part of the terrain collider. So all of the tree colliders and the terrain colliders are merged into one. If you check to see which gameobject you hit, it will give you the terrain collider.

There are some things you can do to know if you hit a tree, or the ground though. If your raycast hit above ground height, but says you hit the terrain gameobject…that means the part of the terrain you hit is higher than ground height, meaning it has to be a tree.

That’s not going to tell you anything about the tree itself, though. You would have to search all of your tree instances assigned to the terrain data, to see which tree was closest to your hit point.
Unfortunately, there are cases when that won’t work. Imagine if you have a tree with a very large radius…say 5. Your hit point would be 5 units away from its center, on the bark right?
Now, what if you had a very small tree close to the large one…closer to the hit point on the large tree trunk that the center of the large tree is to your hitpoint. Your “closest tree to hit point” search algorithm will think you hit the small tree, not the large tree.

Welcome to the terrain system :slight_smile: Another solution is to simply use tree prefabs that you place yourself, and use a lod/culling system like InstanctOC or soemthing. This would give you full control over the trees, but probably at a performance cost. I haven’t tried it YET, but hope to soon.

Hope that helps,
JC

Thanks jc_lvngstn that actually answered my question then.

My intention was to have the player be able to click on a tree and have a unit chop down the tree, yes. Like in some rts games. I’ve been using raycasts to detect when a player clicks on certain units, and I wanted to do the same thing for trees. But now that I know that the collider basically becomes a part of the terrain collider, that is great information to know. I wish maybe they had that information more readily available. It seems to behave contrary to other aspects of collider objects in the scene, so that information is essential I’m sure to many people.

So right now, I am using the terraindata treeinstances[ ] array to instantiate a collider at each tree’s position at the start of the game. I was concerned maybe this would cause a performance problem with so many colliders being instantiated for a large number of trees, but so far it’s working really well.

So I can now “paint” as many trees as I want on my terrain, and via script at the start of the game go through and instantiate a collider at each trees position. I wanted to avoid having to either place each tree prefab individually, or place each collider individually. Thankfully this is possible. As I said, it’s working well right now, but as you know, unforseen problems could arise from this implementation.

So far though, it’s doing the job I need it to do. I just started trying to implement this behavior in my game, and working with terrains, so the information you provided is very very helpful. Thank you. :slight_smile:

Awesome, glad that helped!

Interesting that creating colliders at the trees is working for you. If that becomes a problem (too many), here’s a thought:

If your trees can be somewhat spaced out, you could “pretend” that the terrain is split into tiles. Actually it is, but anyway…
You could make it so that a max of 1 tree was in each tile area. Maybe a 2d array of tiles, some with tree data, some without them.
When you detect you click on a tree (because you hit the terrain collider above ground level), you could use simple math to know exactly which tile it was in. From that 2d array, you would pull your own custom tree data.

Anyway…just a thought, may not be necessary if the collider approach works eh?
Good luck!