- Home /
Stop audio looping on Input.GetButtonUp
I'm a sound designer trying to get a basic grounding in Unity implementation so trying to add audio to the Unity 5 Standard Assets Example Project. I'm a code noob so would appreciate any help here.
I am playing a looping sound while the fire button is held down and currently using audiosource.stop to stop the audio playback when the button is released. However what I would like to do, when the button is released, is not instantly stop the sound but play the audio clip through to the end one last time (it's a very short audio clip - 0.3 seconds) and then stop looping.
Here is my code: The sound is on the audiosource in the inspector and I have loop checked in the inspector also.
var sound : AudioClip;
function Update () {
if(Input.GetButton("Fire1"))
{
if(!GetComponent.<AudioSource>().isPlaying)
{
GetComponent.<AudioSource>().PlayOneShot(sound);
GetComponent.<AudioSource>().Play();
}
}
if(Input.GetButtonUp("Fire1"))
{
GetComponent.<AudioSource>().Stop();
}
}
Is the process to uncheck the loop box in the inspector using the code? Will I need to turn loop back on next time I trigger the sound?
The reason I am doing this is that the sound effect stop instantly causing a popping sound as the waveform does not get a chance to return to 0. Is it a possible workaround to trigger a second sound effect on Input.GetButtonUp that is basically a cool down noise - Ie. a premade fadout of the sound?
My thanks in advance for any help on this topic!
Your answer
Follow this Question
Related Questions
What could make the audio loop? 2 Answers
audio loop when I click the MouseButtonDown(0) 1 Answer
Stereo Mix as Input? 1 Answer
Can not play a disabled audio source 2 Answers
Can I play multiple positioned audiosources with one game object? 1 Answer