- Home /
Sound play on keydown and continue playing
I want to have a car horn on my car. I thought it'd be bast to use this script:
function Update () {
if (Input.GetKey(KeyCode.H)) {
audio.Play();
} else {
audio.Stop();
}
}
But it doesn't work; the audio just stutters. How can i get it that when you press and hold the key, the sound continues, rather that restarting itself?
Answer by Magnus Wolffelt · Aug 08, 2010 at 03:35 PM
Just check if it's already playing, and make sure it's set to be looped:
function Update () {
if (Input.GetKey(KeyCode.H)) {
if(!audio.isPlaying) {
audio.loop = true;
audio.Play();
}
} else {
audio.Stop();
}
}
still won't work, it stops andd starts like a car alarm, what's happening?
don't worry, that script worked, it was the audio loop not set up right.
hey, can anyone help me i am having some audio problems, basically i have a audio on my object which is footsteps. This audio plays when i press the z key which is the walk key. if i press and hold the key the sound plays fine but if i stop holding the key the sounds still carry's on playing until the end can you please help me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AudioServicesPlaySystemSound - iOS System Sounds from Unity 1 Answer
Voice Recording issue ?? 0 Answers
Synch clips with BPM 2 Answers
Play An Audio Clip When An Instantiated Object Collides 1 Answer
Activate sound without Pro filters 0 Answers