Audio not looping.

I Have made a gun and a script, the script is in the gun so that when i click ( Shoot ) the audio plays and you hear a gun shooting ( obviously c: ) so when i click the audio plays, but only ones. Here’s my code :

var MP5K : AudioClip;
 
function Update () {
     if(Input.GetMouseButtonDown(0)) {
          audio.PlayOneShot(MP5K);
     }
}

Who knows which line of code should i add? Or is it just a simple problem which can be fixed by putting something on loop?

[ I Looped it already ]

Thanks.

  • Vince

Hej there. GetMouseButtonDown(0) only fires once if you hold the mouse button. You better try something like

	function Update () {
		if(Input.GetMouseButton(0)) {
			 audio.PlayOneShot(MP5K);
		}
	}

Well you have it set up so there is no way it will be played in a loop.

  1. It’s in an if(Input.GetMouseButtonDown) which only gets called once when you press the button down (Input.GetMouseButton gets called every frame when the button is down).
  2. You have audio.PlayOneShoot which plays the sound only ones.

In my game i do something similar to this in my Fire function (which gets called every frame if the mouse button is pushed):

            if (audio) {
				if (!audio.isPlaying)
					audio.Play();
				audio.loop = true;
			}