How to specify delay time in audio loop?

I’m trying to make a first person shooter game wherein I have an automatic assault rifle with the player (Kalashnikov 47-M). So far, I’ve written a code that will play the gun shot sound once when the left mouse button is clicked, which is below:

void FixedUpdate(){
    if(Input.GetMouseButtonDown(0)){
        AudioSource.PlayClipAtPoint(shot, transform.position);
    }
}

where shot is an AudioClip object. Right now, even if the left mouse button is kept pressed, the shot is fired only once: it’s not continuous. But I want to make it continuous. I tried changing the if to while but unless I put a delay in between consecutive shots, the program obviously hangs. However, the delay SHOULD NOT BE EQUAL TO THE LENGTH OF THE SHOT SOUND CLIP. That’s obvious: when the trigger is kept pressed, the second bullet is fired before the sound of the first bullet has completely ceased. So I want to add a delay of t seconds, where t is LESS THAN THE DURATION OF THE AUDIO CLIP. (t is about 0.5s.)

How to do that? I’m stuck. I tried using the WaitForSeconds(float) function inside an IEnumerator method but that did no help at all. Please help me.

Thanks.

For this behavior I would not recommend playing the sound based on the input.

Instead, write the firing logic based on the mouse click, and have the weapon object play the firing sound each time a bullet is fired. This will keep your sound effect properly aligned to the firing action even when you make changes.

To handle the firing logic, I would just use a simple timer variable. As long as the mouse button is held, decrement the timer by Time.deltaTime. When the timer is equal to or less than 0, fire once (make a bullet, play the sound), then reset the timer variable to its maximum time. changing the maximum time will change the fire rate.

Look into object pooling if your project has automatic weapons or anything else that needs to reuse a lot of prefabs. It will be worth your time.