How to play audio when a key is held (JavaScript)

I need to play audio continuously as long as a key is held down. Thanks :slight_smile:

This would be quite simple. It would just involve defining an AudioSource component, and using AudioSource.Play() on key down. For example:

#pragma strict

var audio : AudioSource; //The AduioSource component.

function Start () {
audio = gameObject.GetComponent(AudioSource); //If the AudioSource component is attached to the same object the script is attached to, meaning it will always find the audio.
}

function Update () {
if (Input.GetKey(KeyCode.Space)){ //The key that will play the audio.
audio.Play();
}
}

This should work,
Namey5.

using c#

void Start(){

List<KeyCode> keyCodes = new List<KeyCode> ();

			keyCodes.Add (KeyCode.W);

			keyCodes.Add (KeyCode.A);

			keyCodes.Add (KeyCode.S);

			keyCodes.Add (KeyCode.D);

			keyCodes.Add (KeyCode.Escape);

			//...etc

}

void Update ()
{

       if(keyCodes!=null){

			foreach (KeyCode keyCode in keyCodes) {

					if (Input.GetKeyDown (keyCode)) {

							Debug.Log ("Play Sound Effect");
					}
			}
		}
}