- Home /
How to loop a sound clip on a ButtonDown function
Hi I need to know how to emit a sound clip when I press the up and down arrow keys, well I found out how to do this, (the script im using to do this is below), but the audio clip im using is a short footstep sound effect so when I click the up or down arrow keys it plays a OneShot function and I know thats because I wrote PlayOneShot, but I need it to loop as long as im holding down one of the keys, and im new at scripting so I don't know how to do this. Please Help, The code below is in JavaScript. If you are going to write a new cod can you please write it in Java, but its ok if you don't. Thanks
var concretefootsteps1Sound : AudioClip;
function Update () { if(Input.GetButtonDown("Vertical")){
audio.PlayOneShot(concretefootsteps1Sound);
}
}
Answer by syclamoth · May 03, 2012 at 01:35 AM
Well, what do you expect 'PlayOneShot' to do?
In any case, I'm pretty sure you don't want your code in Java, since Unity doesn't actually understand that language. If anything, I can give you C# (since that language is syntactically very similar), but I assume you are actually talking about UnityScript. Please don't get them mixed up, it's very annoying.
You should learn C#, btw, because there are much better tutorials for it out there on the internet and moreover it's a language that people actually use (yes, I know Javascript is a very common language, but Unity uses a very specific variant of the language that is pretty much unique).
Change that code to:
function Update()
{
if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1f && !audio.isPlaying)
{
audio.clip = concretefootsteps1Sound;
audio.Play();
}
if(Mathf.Abs(Input.GetAxis("Vertical")) <= 0.1f && audio.isPlaying)
{
audio.Stop();
}
}
Have you found good C# tutorials for Unity Audio? Unity's own documentation seems more focused on using UnityScript.
Your answer
Follow this Question
Related Questions
help sound on collision when key down 2 Answers
Why my sound it's not attaching to my Flashlight 2 Answers
Stop another sound from another script? 1 Answer
How to play Audio Once if enemy has enterd a trigger 1 Answer
Audio Scripting 1 Answer