public class DetectionObject : MonoBehaviour {
public float Reach = 5f;
public GameObject Tree;
private TreeController TC;
// Use this for initialization
void Start() {
TC = Tree.GetComponent<TreeController> ();
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Vector3 fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawRay (transform.position, fwd * Reach, Color.red);
if (Physics.Raycast (transform.position, fwd, out hit, Reach) && hit.transform.CompareTag ("Item") && Input.GetMouseButtonDown (0))
TC.treeHealth -= 100f;
}
}
This is the Code for the Detection component which uses raycast to see what it’s looking at. The problem is that I want to cut down a tree. I can cut down one but when i try to cut down the other it cuts down the original? how can I fix this
Heres the code of the tree:
using UnityEngine;
using System.Collections;
public class TreeController : MonoBehaviour {
public GameObject Log;
public float treeMaxHealth = 100f;
public float treeHealth;
// Use this for initialization
void Start () {
treeHealth = treeMaxHealth;
}
// Update is called once per frame
void Update () {
if (treeHealth < 1) {
OnTreeDestroy ();
}
}
void OnTreeDestroy(){
Instantiate (Log, transform.position, Random.rotation);
Instantiate (Log, transform.position, Random.rotation);
Instantiate (Log, transform.position, Random.rotation);
gameObject.SetActive (false);
}
}