Placing a limit on the number of repeats

public var gunSound : AudioClip;
private var coolTimer : float;

function Start () { }

function Update () {
     if (coolTimer) {
          coolTimer -= Time.deltaTime;
          if (coolTimer < 0)
               coolTimer = 0;
     }
     if (Input.GetButton ("Fire1") && !coolTimer) {
          audio.PlayOneShot (gunSound);
          coolTimer = 0.1;
     }
}

How can gunSound be repeated three times on one press of Fire1?

Not tested, but try this.

   var audioShots : int = 0;
    var GunSound : AudioClip;
    
    function Update () {
        if (Input.GetKeyDown(KeyCode.F)) {
            if (audioShots < 3) {
                audio.PlayOneShot(GunSound);
                audioShots ++;
            }
         }
        if (Input.GetKeyUp(KeyCode.F)) {
           audioShots = 0;
        }
    
    }
    
    audio.loop = false

I don’t know the equivalent of this in C (or even boo), but you can do this in javascript by making a “for” or “while” loop like this:

 //this will loop 3 times (I think)
 for(var i : int = 0, i <= 3, i++) {
    //shoot once
 }

or use a while loop like this:

 var i : int = 0;
 while(i <= 3) {
    //shoot once
    i++;
 }

Please not that both of these parts of code are untested. Also remember it is usually not wise to put this in an update function; it would be better if you put it in a cooroutine.

I hope this helps :slight_smile:

EDIT:

I would not use audio.loop. That is more for background music and sounds. I would use AudioSource.PlayOneShot, but I would instantiate audio sources (empties with that single component, one for every shot), and play each one individually. This is because I’m not sure if it is good to, stop, play, and repeat 3 times in very quick succession on the same audio source, will sound nice.

Hmm… well, there is an efficient and elegant way. Before I proceed with that, however, might I suggest a ‘cheap’ way?

Introducing the ‘cheap’ way:

  1. Edit your audio file via some
    sound editing tool (I’d suggest
    Audacity, it’s free!)

  2. Edit it so that it repeats 3 times
    (or any number of times) in quick
    succession

  3. Import that audio file in Unity, and
    try using the code below (which is
    the exact same code you originally
    posted with just a few
    modifications)

    var GunSound : AudioClip;
    var hasFired : bool;

    function Update () {

    //Play 'looped' clipped when button is pressed
    if (Input.GetKeyDown(KeyCode.F) && !hasFired) {
        hasFired = true;
        audio.clip = GunSound;
        audio.Play();
    
    }
    
    // Make sure you can play the sfx again when button is released
    if (Input.GetKeyUp(KeyCode.F) && hasFired) {
        hasFired = false;
    }
    

    }

Let me know if you still want the efficient and elegant way (which might not be as easy xD)

For anyone in the future who seeks the answer, I wrote some code to help.

public var fireSound : AudioClip;
public var reloadSound : AudioClip;
public var gameLabel : GUISkin;
public var bullets : int;
public var clips : int;
public var burst : int;
private var coolTimer : float;
private var reloadTimer : float;
public var range : float;
public var force : float;

function Update() {
          if (!bullets)
               burst = 0;
	if (coolTimer) {
		coolTimer -= Time.deltaTime;
		if (coolTimer < 0)
			coolTimer = 0;
	}
	if (reloadTimer) {
		reloadTimer -= Time.deltaTime;
		if (reloadTimer < 0)
			reloadTimer = 0;
	}
	if (Input.GetKeyDown (KeyCode.R) && clips && !reloadTimer) {
		audio.PlayOneShot (reloadSound);
		reloadTimer = 2.5;
		clips -= 1;
		bullets = 30;
	}
	if (Input.GetMouseButtonDown(0) && !burst && !coolTimer && !reloadTimer && bullets)
		burst = 3;
	if (burst && !coolTimer) {
		audio.PlayOneShot (fireSound);
		burst -= 1;
		coolTimer = 0.1;
		bullets -= 1;
		}
	}
}