Another delay thread..

Hi guys, I’m having a bit of a problem getting a delay to work.

Even though this has been solved in a fair amount of threads already I still couldn’t exactly understand how to solve this.

This is what I have right now:

var prefabBullet:Transform;
var shootforce = 50;

function Update () {

if (Input.GetButton("Shoot"))
	
	{
		var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
		instanceBullet.rigidbody.AddForce(transform.forward * shootforce);
	}

}

Now I want to have a delay between each shot, I have read a few times, that yield WaitForSeconds would do the trick, but I couldn’t quite get my head around where to put that into the code to get it working…

Whoever can answer my question gets a beer should I ever meet him… :slight_smile:

Something like this should work:

var delay = .5;
private var timer = 0.0;

function Update(){
    timer += Time.deltaTime;
    if (timer >= delay){
        //code to create bullet
        timer = 0.0; //reset timer
    }
}

tonyd, it is very likely that you are the greatest person on earth, thanks for the quick reply. Works just the way I want it to. :slight_smile: