- Home /
Play Audio While Button is Held Down
The title is a little deceiving. I have an 'if' statement that goes like so:
if( Input.GetMouseButtonDown(0) && foobar) audio.Play();
if ( Input.GetMouseButtonUp(0) ) audio.Stop();
the audioSourceis set to 'loop'. This works all fine and dandy, but in a lot of instances FooBar will go from 'false' to 'true' WHILE the player is holding down MouseButton 0. This is a problem for me, becauseif I make it:
if( Input.GetMouseButton(0) && foobar) audio.Play();
The audio will stutter uncontrollably and sound horrific. Any fixes to this problem?
Thanks! - YA
Answer by clunk47 · Dec 18, 2012 at 04:22 AM
Make sure the audio only initializes if it is not already playing.
if(Input.GetMouseButton(0) && foobar)
{
if(!audio.isPlaying)
audio.Play();
}
$$anonymous$$an, Clunk. You are on a roll! Thank you very much! I guess I'm just not thinking like a coder.
You'll get there brotha. It takes a bunch of experimentation with different methods :)
Just take this and it works!
if(!AudioSource.isPlaying)
AudioSource.Play();
Your answer
Follow this Question
Related Questions
Buff System 1 Answer
Code compilation error 1 Answer
Gun State Machine 1 Answer
Play sound when out of ammo 0 Answers