How to delay a shot.

Hi I need to know how to delay a shot. For example I have a game where my player has a gun and I want it so when he holds down the left mouse button the shots fire automatically, with a delay so its not shooting a hundred bullets a second. Heres the code that makes the gun shoot. This code is in java, and if you can please make a public variable so I can change the delay time. The name of the gameobject is laser which you will probably notice in the code. This code does work, but it just needs a delay, and if your wondering I don’t want the if statement to go from Input.GetButton to Input.GetButtonUp or Down. I know that that will make it so the player just has to click it and it will fire, but thats not what I want it to do. I want it the same, but just a delay on when the bullets fire when I hold down the left mouse button. Thanks so much.

var throwSound : AudioClip;
var coconutObject : Rigidbody;
var throwForce : float;

function Update () {

if(Input.GetButton(“Fire1”)){
audio.PlayOneShot(throwSound);
var newCoconut : Rigidbody = Instantiate(coconutObject, transform.position, transform.rotation);
newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce));
newCoconut.name = “laser”;
Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true);

}
}

@script RequireComponent(AudioSource)

Here you are, Unity’s solution examples in JS and C# to solve your problem:

JS

// Instantiates a projectile off every 0.5 seconds,
// if the Fire1 button (default is ctrl) is pressed.

var projectile : GameObject;
var fireRate = 0.5;
private var nextFire = 0.0;

function Update ()
{

if (Input.GetButton ("Fire1") && Time.time > nextFire) 
{
    nextFire = Time.time + fireRate;
    var clone = Instantiate (projectile, transform.position, transform.rotation);
}

}

C#

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour 
{
 public GameObject projectile;
 public float fireRate = 0.5F;
 private float nextFire = 0.0F;

 void Update() 
 {
    if (Input.GetButton("Fire1") && Time.time > nextFire) 
    {
        nextFire = Time.time + fireRate;
        GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
    }
 }
}

Hey! Use a coroutine!

 public void OnClick () {
      if ( canShoot ) {
          StartCoroutine ( shoot() );
      }
}

 public IEnumerator shoot () {

      //Instantiate your projectile
      shootLogic();
      canShoot = false;

      //wait for some time
      yield return new waitForSeconds (cooldownTime);
      canShoot  = true;

}

Cheers!

Overall the coroutine is better. The use of the update function on a shot timer produces varying results and inconsistencies. The enumerator works perfectly.

I tried with coroutine. but unable to get Desired result.

  void startFire()
    {
      
        if (isFiring)
        {
              Debug.Log("firung");

            StartCoroutine(fireCouroutine());

            //obj.transform.parent = transform;
        }
    }

    IEnumerator fireCouroutine()
    {
        Vector3 offset = new Vector2(1, 0);
        GameObject obj = Instantiate(fire, transform.position + offset, Quaternion.identity);
        obj.GetComponent<SpriteRenderer>().color = Color.yellow;
        obj.GetComponent<Rigidbody2D>().velocity = new Vector2(10, 0);

        isFiring = false;
        Debug.Log("1" +isFiring);
        yield return new WaitForSeconds(shootDelay);
       
        isFiring = true;
        Debug.Log("2" + isFiring);

    }

I am calling this frunction here.

if (Input.GetKey(KeyCode.Space) || RightHalf.Contains(touchPos))
        {   
            Debug.Log("fire");
            isFiring = true;
            
             startFire();
           // InvokeRepeating("startFire", 0.003f, 0.4f);
           
            //
        }

Am I doing something wrong.?