Gun Delay [Gun Firing]

So I am programming a script that will allow you to instantiate the bullet, I have changed the GetMouseButtonDown to GetMouseButton, so you can fire it like you could with an assault rifle. But what I failed to realize is that when you hold it down it will build up and you go down to about 2 FPS. I am using a Java Script and I wandered if someone could either; tell me what to add and/or edit themselves and post a reply. This would benefit me a LOT. So without further adieu the CODE:

var projectile : Rigidbody;
var Speed = 10;
var Clone : Rigidbody;
var Cooldown : float;

function Update(){
	if (Input.GetMouseButtonDown(0) && Cooldown == 0){	
		Clone = Instantiate(projectile, transform.position, transform.rotation);
		Clone.velocity = transform.TransformDirection(Vector3 (0, 0, Speed));
		
		Destroy(Clone.gameObject, 10);
}

}

Any help would be greatly appreciated.

Try this:

float	timeLastShot = 0f;
float	delayBetweenShots = 0.2f;

void Update()
{
	 if (Input.GetMouseButtonDown(0) && (Time.time > timeLastShot + delayBetweenShots))
	 {
		timeLastShot = Time.time;
        Clone = Instantiate(projectile, transform.position, transform.rotation);
        Clone.velocity = transform.TransformDirection(Vector3 (0, 0, Speed));
        Destroy(Clone.gameObject, 10);
	 }
}

The above is C#, and it is untested because I do not have Unity3D over here at the moment. The idea is to save the time value when you fire a bullet. Then, in order to make the next shot, the time has to be greater than the last shot’s time plus a delay.

For the above, the delay is 0.2 seconds. So that means 5 shots per second.

Hope this helps.

How I usually do it is using a coroutine:

[Range(.1f, 30f)] public float fireRate;
bool firing;

void Update()
{
 if(Input.GetMouseButtonDown(0) && !firing)
 {
   firing=true;
   StartCoroutine(fire());
 }
}

IEnumerator fire()
{
 while(Input.GetMouseButtonDown(0))
 {
  shootBullet();
  yield return new WaitForSeconds(1/fireRate);
 }
firing=false;

}