How i can add "wait until u can chop again" to my tree chopping script ?

using UnityEngine;
using System.Collections;

public class chopping : MonoBehaviour {
    public Camera myCamera;
    public int Tree_HP=10;
    void Update ()
    {
        if (Tree_HP > 0) {
            if (Vector3.Distance (transform.position, myCamera.transform.root.transform.position) < 5f) {
                if (Input.GetKeyDown(KeyCode.Mouse0)) {
                    Ray ray = new Ray (myCamera.transform.position, myCamera.transform.forward);
                    RaycastHit hit;
                    if (Physics.Raycast (ray, out hit, 5f)) {
                        if (hit.collider.gameObject == gameObject) {
                            Tree_HP--;
                            GetComponent<AudioSource>().Play();


                        }
                    }
                }
            }
        }
        if (Tree_HP <= 0) {
            Tree_HP = 0;
            Destroy (gameObject);
        }
    }
}
float chopWait = 5; // chop every 5 seconds
float lastChopTime; // last time we chopped

void Update()
{
    if (Tree_HP > 0 && Time.time - lastChopTime >= chopWait)
    {
        //....
        if (Physics.Raycast(.....))
        {
            lastChopTime = Time.time;
            Tree_HP--;
             //.......
        }
    }
}

There is problem with Time.Time

There should be lastChopTime = Time.time;

  • Thanks :slight_smile: it’s working

You’re right - edited my original post.