- Home /
Play Oneshot Audio On Touch
Hello everyone!
I have a script that plays audio on touch, and in that sense it works just fine. I have been working to make it so that when you click something to play an audio file, you can't que the audio file again while it's playing. But the way the script is now, the audio never plays again at all, ever. Even after the audio is done playing. Although sometimes right after finishing playing it will let me click it again. It's finicky! Take a look if you have a moment:
public class PlayAudioOnTouch : Touchable
{
private int _timer = 0;
public override void OnStartTouch ()
{
if(_timer == 0)
{
if( audio != null ) audio.Play();
base.OnStartTouch();
_timer = 1;
}
else if(_timer > 0 && _timer <= Time.time)
_timer++;
else
_timer = 0;
}
public override void OnEndTouch ()
{
base.OnEndTouch();
}
}
If I were to put money on the source of the problem. I would say use of "Time.time" is what's cuasing these issues. I think using something else in lue of that would work better, but I don't know what to use. Any input would be more than welcome. Thanks for taking the time to look at my question!
I'd recommend extending your class with $$anonymous$$onobehavior and fetching the AudioSource component from whichever GameObject you want to play sounds. By doing this you can track a lot of info, also when the AudioSource is playing and when it's not
Thanks for the suggestion! I'll see what I can make happen by doing what you said.
Your answer
Follow this Question
Related Questions
play audio on touch 1 Answer
touch the object to play an audio 1 Answer
Randomly play audio 0 Answers
make audio play on click 1 Answer