0
\$\begingroup\$

You will probably laugh at my code here (and I would love to hear some completely different approaches to this problem if possible), but I've written this myself using the only logic I could come up with alone.

Basically I want to make a slope randomly covered with trees. The slope is a quad with a mesh collider (I've also tried Box), and the tree is a prefab with just a model and a collider. I iterate through every possible tree location and give it a 0.25 chance of spawning a tree. This all works ok, but since the quad is not lying flat to any axis, I couldn't think how best to assign the Y-position value to my trees.

My idea was to simply drop the trees all from above and have them land in the quads collider. If I lay the quad flat my code will work fine, but as soon as I tilt the slope the collision is still detected. It seems as if its reading the top of the quads highest point and making a box around it all the way to the bottom.

This is the code:

        for (int i = 0; i < trees.Count; i++)
        {
            if (!trees[i].GetComponent<Collider>().bounds.Intersects(ski_slope.GetComponent<Collider>().bounds))
            {
                Debug.Log("moving tree");
                trees[i].transform.Translate(0, -0.2f, 0);

            }
        }

So really I think my use of the 'Intersects' function must be wrong.

I hope my description of the problem makes sense. Otherwise I can take some screenshots to try show it.

Thank you for any help.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This is one of those many situations where reading the documentation helps.

The documentation for the Bounds type tells you that it:

Represents an axis aligned bounding box.

An axis-aligned bounding box, or AABB for short, is a box aligned with coordinate axes and fully enclosing some object. Because the box is never rotated with respect to the axes, it can be defined by just its center and extents, or alternatively by min and max points.

The documentation for Collider.bounds tells you it's:

The world space bounding volume of the collider (Read Only).

Note that this will be an empty bounding box if the collider is disabled or the game object is inactive.

So when you say:

It seems as if its reading the top of the quads highest point and making a box around it all the way to the bottom.

Yes, that is exactly what you asked it to do. Asking for Collider.bounds says "find the highest, lowest, northmost, eastmost, southmost, and westmost points of this collider and give me a box aligned with the world axes enclosing those extremes".

Did you mean to use a raycast, spherecast, or boxcast sweeping downward to find the first intersection point with your mesh collider instead?

\$\endgroup\$
1
  • \$\begingroup\$ Thank you so much.... Much regret not studying the docs first :] You are very helpful! I have used Raycast before but completely forgot about them LOL. This should work perfectly. \$\endgroup\$ May 22, 2021 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .