**I have two scripts, one to give the full tree health (Tree Health), which when it reaches 0 spawns a chopped tree. This works correctly and as expected, but when the chopped tree is spawned, the top half of the tree does not fall over D:! I have applied a capsule collider, a rigid body and the script (Push Tree) to the part of the tree that should fall (the stump stays) but it doesn’t seem to fall. I have also applied a capsule collider to the stump of the cut tree. Anyway, here are the two scripts (C#):
Tree Health:
**
using UnityEngine;
using System.Collections;
public class TreeHealth : MonoBehaviour {
public int Health;
public GameObject FallenTree;
public Camera myCamera;
void Start () {
myCamera = GameObject.FindObjectOfType<Camera>();
}
void Update ()
{
if(Health > 0)
{
if(Vector3.Distance(transform.position, myCamera.transform.root.transform.position) < 15f)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Ray ray = new Ray(myCamera.transform.position,myCamera.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,15f))
{
if(hit.collider.gameObject == gameObject)
{
--Health;
}
}
}
}
}
if(Health <= 0)
{
Health = 0;
Destroy(gameObject);
Instantiate(FallenTree,transform.position,transform.rotation);
}
}
}
And here is the Push Tree script I am presumably having problems with:
using UnityEngine;
using System.Collections;
public class PushTree : MonoBehaviour {
public float Force;
public Camera MyCamera;
void Start ()
{
MyCamera = GameObject.FindObjectOfType<Camera>();
GetComponent<Rigidbody>().AddTorque(MyCamera.transform.right * Force,ForceMode.Impulse);
}
// Update is called once per frame
void Update () {
}
}
If no one can find the problem in this code then any suggestions about the prefabs would also be gladly appreciated!