- Home /
How do I get the sound effect to continue playing?
Hi I am completely new to Unity and programming in general and have run into a problem that I couldn't fix through hours of searching for solutions and work-arounds on google.
I am trying to get footsteps to play for my FPS game. Currently I have the footsteps sound on an Audio Source component on my FirstPersonPlayer GameObject. I have checkedmarked the "Loop" option.
Here is the script I use to control when the sound plays:
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Horizontal"))
{
audioSource.Play();
}
if(!Input.GetButtonUp("Horizontal"))
{
audioSource.Stop();
}
if(Input.GetButtonDown("Vertical"))
{
audioSource.Play();
}
if(Input.GetButtonUp("Vertical"))
{
audioSource.Stop();
}
}
}
Currently, when I'm holding "W" to move forward the sound plays, as it should. However, while holding down the "W" key, if I press any other directional key such as "A" or "D" and then release "A" or "D" while still holding down "W", the noise will stop altogether when it should still play since "W" was not released. How do I fix this issue? I have tried switching the "GetButtonDown" to "GetButton" as well as "GetAxis" but both of those don't even play the footstep sound in the first place. Thank you for your time and have a nice day.
Is there any specific reason for having that ! on the second if ?
if(!Input.GetButtonUp("Horizontal"))
{
audioSource.Stop();
}
Answer by Jon_Olive · Jun 25, 2020 at 08:51 AM
if(!Input.GetButtonUp("Horizontal"))
@amgitskevinshih You're testing here if the input isn't the horizontal button going up - so it will be true if any other input is received - i.e. any other key is pressed or released. Remove the ! and it should work.