How can I destroy terrain trees in unity by script?

Started by
3 comments, last by sheffieldlad 3 years, 8 months ago

I placed many trees with a terrain.

And now I want to destroy one tree if any object hits the tree.

But the tree dont executes the script.

Advertisement

Can you show us your script?

Assuming your trees have colliders attached to them, then on your object that will be hitting the trees you add a collider as well. Then you can simply follow something like this (from the unity documentation): https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html​ to then simply call Object.Destroy on the tree.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

/// <summary>
/// AS_TreeTerrain written by Paul Tricklebank.
/// Add this script to a terrain positioned at 0,0,0 and do not add tree colliders.
/// This script will generate colliders and add the tree damage script to each tree instance.
/// Because changes to the terrain at runtime are saved well, not 100% accurate,
/// Changes made at runtime are not reversed this script records information about
///Tree instances and replaces them at the end of runtime.
/// use for your own purposes. no credit required.

/// </summary>

using UnityEngine;
using System.Collections;

public class AS_TreeTerrain : MonoBehaviour {
public Terrain terrain;
private TreeInstance[] _originalTrees;


void Start () {
terrain = GetComponent<Terrain>();
// backup original terrain trees
_originalTrees = terrain.terrainData.treeInstances;
// create capsule collider for every terrain tree
for (int i = 0; i < terrain.terrainData.treeInstances.Length; i++) {
TreeInstance treeInstance = terrain.terrainData.treeInstances[i];
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
CapsuleCollider capsuleCollider = capsule.collider as CapsuleCollider;
capsuleCollider.center = new Vector3(0, 5, 0);
capsuleCollider.height = 10;
DestroyableTree tree = capsule.AddComponent<AS_DestroyableTree>();
tree.terrainIndex = i;
capsule.transform.position = Vector3.Scale(treeInstance.position, terrain.terrainData.size);
capsule.tag = "Tree";
capsule.transform.parent = terrain.transform;
capsule.renderer.enabled = false;

}
}
void OnApplicationQuit() {
// restore original trees
terrain.terrainData.treeInstances = _originalTrees;
}
}

Add this script to the terrain. terrain must be at 0,0,0.

uncheck add tree colliders.

The above script will add the next script to each tree.

/// AS_DestroyableTree written by Paul Tricklebank.
///
/// This script gets added to each tree instance and handles tree damage and removal of tree instances.
/// use for your own purposes. no credit required.
/// </summary>

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AS_DestroyableTree : MonoBehaviour {
public int terrainIndex;
//the prefab to instatiate when a tree is removed.
//this prefab is loaded from the Resources folder
private GameObject Log;

//Each trees vp_HealthPickup level.
public float health;



void Start()
{
//set the trees health
health = 20;
//set the log prefab
Log = Resources.Load<GameObject>("choppedTree"); //change this line and add your own prefab which will be instantiated when a tree is cut down
}


public void Damage(float damage)

{
//deplete health. if it falls below 0 destroy the tree
health = health - damage;


if(health <=0)

{
Delete ();
}
}


public void Delete() {
//remove the tree and instantiate the log prefab
Terrain terrain = Terrain.activeTerrain;
List<TreeInstance> trees = new List<TreeInstance>(terrain.terrainData.treeInstances);
trees[terrainIndex] = new TreeInstance();
terrain.terrainData.treeInstances = trees.ToArray();
Instantiate (Log, this.transform.position, this.transform.rotation);
Destroy(gameObject);
}


}

I wrote this a long time ago. It's based on a tutorial by someone who's name escapes me (Sorry.) Works great on Unity 4 or 5.

It may or may not still work on later versions.

Good luck.

This topic is closed to new replies.

Advertisement