# How do I play an audio clip repeatedly, controlled by time?

I have a gun firing sound audio clip and I want this to be played repeatedly while the mouse left button is pressed, like a machine gun where there is automatic firing.

If I do something like:

void Update()
{
if (gunScript.gotGun)
{
var mouse = Mouse.current;

if (mouse != null)
{
if (mouse.leftButton.isPressed)
{
gunFireSound.Play();
}
}
}

}


It plays extremely rapidly as that's how the code gets executed very fast. How do I add a small delay or is there a better way to achieve this?

I also thought of an alternative but it doesn't play it fast enough. (the alternative was an if statement to check if it is finished playing, and play it again)

• Does your audio clip play a single-fired shot, or is it a looping sound? You can deal with these two cases in two different ways. Jun 16 '20 at 12:13
• It is a single fired shot. Maybe I should do what DMGregory said and put the file in Audacity and get it so that the looping sounds right, but I'll try the other solutions first. Jun 16 '20 at 12:16

You can manage this using a coroutine. For example:

private IEnumerator GunShotSound_Coroutine()
{
while(mouse.leftButton.isPressed)
{
gunFireSound.Play();
yield return new WaitForSeconds(1.0f);
}
}


In the update function, you controlled if the leftbutton was pressed this frame and you start the coroutine.

if(Input.GetKeyDown(mouse.leftButton))
{
StartCoroutine("GunShotSound_Coroutine");
}


I did not test this code but the idea is here :)

• Better to remove the quotation marks around the coroutine name so you can reference it directly, without reflection. This will also let it update correctly as code is refactored. Jun 16 '20 at 12:07
• Brilliant! This seems to be the fastest solution, I will check back on it. Jun 16 '20 at 12:18
• I just used the code and changed the seconds to 0.1f and it's perfect! Works like a charm, thank you all. :D Jun 16 '20 at 12:25

The simplest thing to do is to check whether the audio source is still playing the previous sound, and play a new one only if it's done:

if(!gunFireSound.isPlaying)
gunFireSound.Play();


But that might make your shots sound too far apart. So you might want to allow your new shot to interrupt the old one after a specified duration.

if(!gunFireSound.isPlaying || gunFireSound.timeSamples > gunFireInterrupt)
gunFireSound.Play();


Here timeSamples is a high-precision count of the individual audio samples played so far.

You can also use PlayScheduled to queue-up the next iteration of the shot at a fixed interval from the previous one, to make the rhythm of shots more uniform despite variances in frame timing.

But the best solution is probably to have a looping machine gun sound that you can start playing on press, and transition into a tail-off sound on release. This will give you the best control over the sound and rhythm of the shots, and avoid artifacts from interrupting a sound in progress.

• Thanks! I am a little confused over the timeSamples and gunFireInterrupt. How do I control the timeSamples and the other variable if you don't mind me asking? :) Also that sounds like a great idea, I will see if I can get it to work that way. Jun 16 '20 at 12:17
• You just set gunFireInterrupt to an integer high enough for your needs. For instance, a 44 kHz audio clip uses 44 000 samples per second. So if you want to be able to interrupt it after half a second you'd set gunFireInterrupt to 22 000 Jun 16 '20 at 12:20
• I see, thank you very much! I will look into this more. Jun 16 '20 at 12:24
• The looping sound approach works best for weapons with high ROF, whereas I prefer to sync the SFX playing to the actual logic (e.g. instantiating the bullet object/trail/whatever) for semi-automatic weapons or low ROFs. Jun 16 '20 at 12:32