- Home /
Question by
Pritamde12 · Jun 21, 2013 at 04:33 PM ·
inputaudio source
Audio Stutter
I need to play an audio when a button is held down and stop the audio when any button is not held down. Its stuttering with the following code.
function Update () {
if(Input.anyKey ){
if(!audio.isPlaying)
{
audio.Play();
}
else
{
audio.Stop();
}}}
Without the else statement the audio doesnt stutter. But i need to play audio only when the button is down. Thanx in advance.
Comment
Best Answer
Answer by AntiLunchBox · Jun 21, 2013 at 07:38 PM
that Else statement has to hook up to the if(Input.anyKey) statement. It's a lot easier to notice when you indent correctly. SO:
function Update () {
if(Input.anyKey ){
if(!audio.isPlaying)
{
audio.Play();
}
}
else
{
audio.Stop();
}
}