How to play a reload sound when "r" pressed and clip empty (Using Dastardly Banana's FPS Package)

I’m using Dastardly Banana’s download, “FPS Package”, and I want to edit it slightly.

Through mass amounts of trial and error (and there was a lot more of the latter…), I’ve got the gun to play “Gunshot_Pistol” (Audio Clip) when the left mouse button is pressed by doing the following:

var Gunshot_Pistol : AudioSource;



function Update () {

   if(Input.GetButtonDown("Fire1")){

      Gunshot_Pistol.Play();



   }

}

However, this was purely luck as I Googled how to do it. I have absolutely no scripting knowledge at all, and none of it makes sense to me. I used to use Blender with Logic Bricks until constantly-dropping framerate became an issue, so I switched to Unity.

So, any help with the following would be greatly appreciated. I need to know EXACTLY:

  1. How to tell Unity to play “Reloading.wav” (Audio Clip) when my gun reloads EITHER when I press R or when it runs out of bullets in the clip

  2. Where to put the AudioSource

If you wouldn’t mind, a step-by-step guide would be amazing. I’m really new to Unity and it’s great, but I just want my game to work properly.

Thank you in advance.

You could add the AudioSource component to your character, then define the audio clips using AudioClip variables. You should also control the ammo: if you have bullets, shot; if run out of bullets but still have clips, reload; if no more clips or bullets, play the click sound - something like this:

var GunShotPistol: AudioClip; // drag the Gunshot_Pistol sound here
var Reload: AudioClip; // drag the Reload sound here
var Click: AudioClip; // drag the empty gun click here
var bullets: int = 6; // bullets in gun
var clips: int = 3; // how many clips you have
var bulletsClip: int = 6; // six bullets per clip

function Update () {
    if (Input.GetButtonDown("Fire1")){ // when pressing the fire button...
        if (bullets > 0){ // AND you have bullets...
            audio.PlayOneShot(GunShotPistol); // play the sound
            bullets -= 1; // and decrement bullet count
        } else { // but if the bullets are over
            audio.PlayOneShot(Click); // play the click sound
        } 
    }
    if (Input.GetKeyDown("r") || bullets < 0){ // "r" pressed OR no bullets...
        if (clips > 0){ // AND you have clips
            audio.PlayOneShot(Reload); // play the reload sound
            bullets = bulletsClip; // and reload bullet count
            clips -= 1; // decrement clips
        }
    }
}

If you don’t want the click sound, just remove the else {…} part.

You can modify all variables above in the Inspector: if you want the clip to have 10 bullets, for instance, just change it at the Inspector. If you modify the variable during the game, the modification will be lost when the game ends; if you modify before running the game, however, the Inspector will save the new value and - be aware! - use it every time after, even if you change the value in the script! - this feature has been driving many experienced programmers to madness, so remember that!