- Home /
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
Answer by ExTheSea · May 12, 2013 at 05:21 PM
Well you have it set up so there is no way it will be played in a loop.
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).
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;
}
Well thanks anyways but i have solved it completely. ( Will be sad if i dont know still in the future ) but i made not a machinegun but shotgun so that the sound only has to be played once.
Now you should either close the question or accept an answer to mark your question as solved.
Answer by Mexallon · May 12, 2013 at 05:17 PM
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 thanks anyways but i have solved it completely. ( Will be sad if i dont know still in the future ) but i made not a machinegun but shotgun so that the sound only has to be played once.
Your answer