- Home /
How to play loop an audio clip on button hold down?
Hi I have this code(code below) that is working, but I don't just want it to play the audio once then stop. I need it so that when I hold down the left, right, up or down arrow keys it will loop the my audio clip which is about 1 to 2 seconds long, but stop when the arrow keys are not being pressed. It's my characters walking sound by the way and the audio clip I'm trying to play is a footstep sound. Also Please do it in java. Thanks
Here's the code:
var sound : AudioClip;
function Update () { if(Input.GetButtonDown("Fire1")) { audio.PlayOneShot(sound); audio.Play(); } }
Answer by ByteSheep · Mar 11, 2013 at 05:04 PM
Just add a simple check to see if the audio is already playing:
var sound : AudioClip;
function Update () {
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if(!audio.isPlaying)
{
audio.PlayOneShot(sound);
audio.Play();
}
}
}
Here's an alternative that lets you control how much of the audio clip you want to play before looping:
var sound : AudioClip;
var timer : float = 100.0;
var clipLength : float;
function Start() {
//clip the end of the audio file by 0.2 sec
clipLength = sound.clip.length - 0.2;
}
function Update () {
if(audio.isPlaying)
{
timer += Time.deltaTime;
}
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if(timer >= clipLength)
{
audio.PlayOneShot(sound);
audio.Play();
timer = 0.0;
}
}
}
That worked merry_christmas it does exactly what I wanted, but it just waits to long before it plays it again and I want it to play on w,s,d,a.
I edited it to play when the arrow keys or w,a,s,d keys are pressed..
The audio file is looping, so if you want it to be shorter you will have to shorten the audio file.
Or it could also be done quite easily through code if you'd like an example.
You can mark my answer as accepted by clicking the green tick if it has answered your question so others will know the question has been solved..
Thank you so much merry_christmas for all your help it works great! Thanks BlackWIngsCorp your answer worked to, it just didn't do what I wanted, but thanks for the help(:
By the way if it's not to much trouble could you convert this to C# for me? If not thats ok you have already done more than enough.
Well having someone else convert it for you won't help you learn C#. The conversion is easy enough, just remember the following:
• in C# functions need to be declared with the type of data they will return, e.g. function Start() would be void Start()
• javascript variables are declared like var VariableName : VariableType; and in C# like this VariableType VariableName;
e.g. var testvar : boolean; would become boolean testvar;
Those are the main things you will have to keep in $$anonymous$$d! If you still have problems converting it then please post back with what you have managed to convert so far.
Answer by BlackWingsCorp · Mar 11, 2013 at 03:50 PM
Try audio.loop(sound);
Thanks the audio.loop(sound); just gives me the error below and merry christmas's answer made the sound play while I'm holding down the button, but it plays to fast, I need it to fully play the clip before it starts playing it again and nobody really did what I asked. The reason I gave so much information is so that someone can right a code example, I am not very good at coding yet.
How much time does the sound take before it ends? Let's say 3 secs. Put something like this: PS: watch out for the brackets and tell me how it works
vat timer : float = 4
if(Input.GetButtonDown("The Button")){
if(timer == 0){
audio.PlayOneShot(sound);
timer -= Time.deltaTime;
}
if(timer == 0){
timer = 4;
}
}
Answer by saik007 · Jun 17, 2016 at 03:57 PM
void Update () { if (Input.GetKeyDown (KeyCode.UpArrow)) {
sample.Play ();
} else if (Input.GetKeyUp (KeyCode.UpArrow)) {
sample.Stop ();
}
Your answer
Follow this Question
Related Questions
How to play several audio cllips one after another. 3 Answers
How to play an audio one shot clip on a collision 1 Answer
Why my sound it's not attaching to my Flashlight 2 Answers
Stop another sound from another script? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers