I am trying to make my machine gun fire when you hold down the mouse button, but right now it shoots like 100 bullets in one second. So I wanna put i delay on, so it takes a couple of tenths of a second before you can shoot again - this is where my annoying problem comes in. Right now it shoots one time, then stops for 0.12 sec and then starts shooting like a madman again. Please help me fix this code
var effect : Transform;
var next : boolean = false;
var shootSound : AudioClip;
var shot : boolean = false;
function Update () {
if(Input.GetMouseButton(0)){
AudioSource.PlayClipAtPoint(shootSound, transform.position);
if(shot == false){
shot = true;
Shoot();
return;
}
else if (shot == true){
wait();
}
}
}
function Shoot(){
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
yield WaitForSeconds(0.12);
if(Physics.Raycast(transform.position, fwd, hit)){
if(hit.rigidbody){
hit.rigidbody.velocity.z += 100;
}
var sparks = Instantiate(effect, hit.point, transform.rotation);
Destroy(sparks.gameObject, 0.5);
}
}
function wait(){
yield WaitForSeconds(0.4);
shot = false;
}
Thanks