- Home /
Audio problem? "Or" syntax question?
Hello everyone,
I hope this is an ok question to ask, I am trying to make a footsteps/jumping audio code. The code I've come up with is below. It works pretty well, the footsteps audio pauses to play the jump audio when the jump button is pushed while the run keys are held.
var Footsteps: AudioClip;
var Jump: AudioClip;
function Update () {
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical")){ //If pressing the move keys
if(!audio.isPlaying){ //And audio is not playing
audio.clip=Footsteps; //Find the Footsteps Audio
audio.Play(); //And play it
if(Input.GetButtonDown("Jump")){ //If the jump key is pressed
audio.Stop(); //Stop playing footsteps. (Jump audio initiated below
}
}
}
else if ( !Input.GetButton( "Horizontal" ) || !Input.GetButton( "Vertical" ) && audio.isPlaying ){ //Else if not pressing move keys
audio.Stop(); //Stop playing audio
}
if (Input.GetButtonDown("Jump")){ //If pressing jump button down
audio.Stop(); //Stop any other Audio
audio.clip=Jump; //Find the Jump audio clip
audio.Play(); //Play the Jump clip
}
}
My problem is that if the run keys /aren't/ pressed and the jump audio doesn't play. I've been staring at this for a while and I can't seem to figure it out. My question is am I not understanding the function of "Or" ( || ) correctly? To my untrained eyes it looks like whenever the jump button is pushed the audio should play, regardless of other button pushes.
Thanks for any help!
Christina
Answer by ATMEthan · Aug 26, 2013 at 01:05 PM
I believe this line right here
else if ( !Input.GetButton( "Horizontal" ) || !Input.GetButton( "Vertical" ) && audio.isPlaying ){ //Else if not pressing move keys
audio.Stop(); //Stop playing audio
}
stops your audio from playing. Even though you play the jump audio a couple lines after. In the next update, which happens nearly instantly, stops the audio from playing. If I'm reading this correctly your second if will stop all audio if you are not moving left or right and audio is playing, hence if you just jump nothing plays.
I think its how you handle your if statements that's messing you up.
I would structure it the following way, see if this helps out at all.
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")
//do what u gotta do
else if (Input.GetButtonDown("Jump"))
//do what u gotta do
else
//audio.Stop()?
You star that's exactly what was happening! Thanks I'd been staring at that for ages! I've done pretty much what you said and added a little if statement in to see if the audio playing is "Footsteps" or "Jump", works brilliantly.
Thanks so much for the help! X
Your answer
Follow this Question
Related Questions
Calling an if statement once even when it is calling every frame? 3 Answers
Calling myfunction from update function 1 Answer
Simple && and || question. 1 Answer
play an audio clip in an if statement 2 Answers
Help with a simple conversation script? 0 Answers