How can I limit the length of a one-shot sound when changing the actual file is not enough? Do I have to flick mute on and off?
1 Answer
1You can use audio.Stop() as well. It’s better because it really stops the sound. With mute, the sound continues playing - and it will be “ressurected” if you fire a second sound before it has ended.
You can use a coroutine to do it in a painless fire and forget fashion:
function PlaySoundLimited(sound: AudioClip, duration: float){
audio.PlayOneShot(sound);
yield WaitForSeconds(duration);
audio.Stop();
}
You say you want to limit the length - does this mean make the entire audio clip play faster, or stop the audio clip before it's finished playing?
– Julien-Lynge