How to make a gun shoot in burst.

Hello, you. Yes you!

I have searched a little bit, but I am unable to find and questions or answers about “Bursts”. For those that do not know burst are(in this case) 3 shot fired with just a slight delay between each other when the trigger is pulled.

I tried something like this:

var sound : AudioClip;

function Shoot () //Ran from function Update.
{
audio.PlayOneShot(sound);
yield WaitForSeconds(0.2);
audio.PlayOneShot(sound);
yield WaitForSeconds(0.2);
audio.PlayOneShot(sound);
}

But no luck now the function wont execute at all, but even if it did it will look ugly and “not cool”. I know I can use for´s for(var blah blah), but there is no delay between those.

So dear community is there a way in a relatively easy manner that we can create a burst effect?

Legion.

I would do something like this:

int burstShots = 0;
float burstTimer = 0.0;
float burstDelay = 0.0;

function Update()
{   
    if(burstShots > 0) {
        if(burstTimer <= burstDelay) {
            // play the sound or whatever
            burstTimer = burstDelay;
            burstShots -= 1;
        } else {
            burstTimer -= Time.deltaTime;
        }   
    }   
}

You could also use the ‘while’ loop:

int i = 3;
float waitTimer;

while (i > 0 && waitTimer <= 0) {
     audio.PlayOneShot(sound);
     waitTimer = 0.2f;
}