- Home /
How to play loop an audio clip on button hold down?
var sound : AudioClip;
function Update (){
if(Input.GetButtonDown("Vertical")){
audio.PlayOneShot(sound);
}
}
While pressing the play sound only once. What should I add the code for that sound plays over and over again if I hold the key?
Comment
Answer by Seizure · Sep 23, 2013 at 03:15 PM
using UnityEngine;
using System.Collections;
public class playSound : MonoBehaviour {
public AudioSource mySound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Mouse0))
{
mySound.enabled = true;
mySound.loop = true;
}
else
{
mySound.enabled = false;
mySound.loop = false;
}
}
}
If you follow this answer. $$anonymous$$ake sure you have the audioSource PlayOnAwake set to true, or it wont work.
Your answer
Follow this Question
Related Questions
Always play music? 1 Answer
Play sound on anim frame 1 Answer
I want my trigger sound only to play once! 0 Answers
How an Enemy could activate a Sound? 3 Answers
Play Looped Sound? 4 Answers