0
\$\begingroup\$

I am developing RTS game. When I build buildings on the Terrain, any trees that were in that location end up intersecting with the building.

I want to remove all trees from the building's footprint when placing.

Can I use the building's OnTriggerStay to detect tree colliders with a particular tag and destroy the trees? Or is there a better way to clear out these trees?

\$\endgroup\$
5
  • \$\begingroup\$ I don't think OnTriggerStay is the right tool for this job, since I wouldn't expect either your trees or the buildings to have Rigidbodies. At least one of the colliders needs a dynamic body to fire trigger messages. It sounds like you want something more like Physics.OverlapBoxNonAlloc to get an array of all the trees overlapping your building's location. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2019 at 18:25
  • \$\begingroup\$ I am in a poor level in the programming I can for now get trees in Terrain what about Physics.OverlapBoxNonAlloc It was something advanced \$\endgroup\$ Commented Jul 30, 2019 at 18:53
  • \$\begingroup\$ I don't understand your question. I recommend reaching out to a friend who has more practice with English to ask if they can help interpret or proofread for you, to he sure you're understood accurately. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2019 at 19:04
  • \$\begingroup\$ Excuse me for a very me bad language to notice this after Edited question \$\endgroup\$ Commented Jul 30, 2019 at 19:06
  • \$\begingroup\$ I'll try Physics.OverlapBoxNonAlloc thank you \$\endgroup\$ Commented Jul 30, 2019 at 19:09

3 Answers 3

1
\$\begingroup\$

Here is my solution:

void Update()
{

    TerrainData terrain = Terrain.activeTerrain.terrainData;
    ArrayList instances = new ArrayList();

    foreach (TreeInstance tree in terrain.treeInstances)
    {
        distanceG = Vector3.Distance(gameObject.transform.position, tree.position);

        distance = Vector3.Distance(Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, transform.position);

        if (distance < closeRange)
        {
            // the tree is in range - destroy it
            print("1");
        }
        else
        {
            // tree is out of range - keep it
            instances.Add(tree);

        }
    }
    terrain.treeInstances = (TreeInstance[])instances.ToArray(typeof(TreeInstance));

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might want to mark this answer as the accepted one :) \$\endgroup\$
    – Vaillancourt
    Commented Aug 25, 2020 at 0:58
0
\$\begingroup\$

When you place the building you could search for any tree object in a certain radius around it and if it's less than a certain distance delete them. That way you wouldn't need collision.

You would need a reference to the trees in the scene to access their position information.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That looks to be what NADER LABBAD is already doing in the answer from a year ago, no? \$\endgroup\$
    – DMGregory
    Commented Aug 24, 2020 at 23:28
  • \$\begingroup\$ My bad, didn't even notice the date. I'm still getting used to this sites UI \$\endgroup\$ Commented Aug 24, 2020 at 23:57
0
\$\begingroup\$

Assuming your buildings have a rectangular base, you can use Physics.OverlapBox / Physics2D.OverlapBox to get all the colliders which overlap the area where you want to put down the building and decide what to do with them depending on object type.

\$\endgroup\$

You must log in to answer this question.

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