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.