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\$
  • \$\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 Jul 30 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\$ – NADER LABBAD Jul 30 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 Jul 30 at 19:04
  • \$\begingroup\$ Excuse me for a very me bad language to notice this after Edited question \$\endgroup\$ – NADER LABBAD Jul 30 at 19:06
  • \$\begingroup\$ I'll try Physics.OverlapBoxNonAlloc thank you \$\endgroup\$ – NADER LABBAD Jul 30 at 19:09
0
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.