WaitForSeconds - Shooting Script

I Have a REALLY basic script in C# that basically spawn a bullet, and then move it. (Basically a shoot)

It doesn’t matter if you click the mouse button as fast as you can, it will not shoot so fast, but it still is fast, and when i make the reloading system, it will be a chaos. So, i’ve been trying to put a delay in the script.

Example:
THE BULLET IS SPAWNED.
THE BULLET IS SHOOT.
WAIT FOR 1.3 SECONDS
ANOTHER BULLET IS SPAWNED
ANOTHER BULLET IS SHOOT
ETC…

But i can’t get it to work! I cannot understand what to do! I already read the manual but it doesn’t help a all, as (in my opinion), it’s saying to me to create a script to make another script to work or something like that. I can’t figure out what i need to do! Can someone explain me how can i do this? I would appreciate if anyone could complete the script =|

using UnityEngine;
using System.Collections;

public class ShootDemo : MonoBehaviour
{
public Rigidbody projectile;

public float speed = 0;

// Update is called once per frame
void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
        instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    }
}

}

The code below is from Unity documentation on using Time.time - it may be just what you are looking for. Try adding in your code to this or vice versa. It adds a simple countdown timer that resets a countdown before being allowed to fire again

using UnityEngine;
using System.Collections;

public class ExampleClass : 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;
        }
    }
}

You can check the status of boolean variables that decides whether bullet can be fired or not.

Like, we have boolean variable named canFire with default value true. When bullet will be fired it is to be set to false. After decided time (in your case 1.3 secs) it will be set to true so bullet can be shoot again.

public float speed = 0;
	bool _canFire=true;

	// Update is called once per frame
	void Update()
	{
		if (Input.GetButtonDown("Fire1") && _canFire)
		{
			Fire();
		}
	}

	void Fire()
	{
		Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
		instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
		_canFire=false;
		StartCoroutine(WaitToEnableFire());
	}

	IEnumerator WaitToEnableFire()
	{
		yield return new WaitForSeconds(1.3f); //decided time to wait
		_canFire=true;
	}

Untested, but it should work:

 public float speed = 0;

float timer;

public float fireRate;
// Update is called once per frame

void Update()
{
timer += Time.deltaTime;
if (Input.GetButtonDown(“Fire1”) && timer >= fireRate)
{
timer = 0;
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}