Skip to main content
Formatting
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Blockquote void Update() {

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));

}

After a long time it works

Blockquote 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));

}

After a long time it works

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));

}
Source Link

Blockquote 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));

}

After a long time it works