audio script motor sound

I am using this script to make a motor acceleration sound.
i have one audio source for idel and set up a second one for
a higher motor sound for when its running. I have been trying
to get it to play on key press and stop when i release the key
but cant seem to get it to work. i know the audio play one shot
will only play it once but i cant seem to change i so it works
any help would be be apreicated,

var audio01 : AudioClip;
function Update () {   
    if (Input.GetKeyDown ("up"))
   {
      audio.PlayOneShot(audio01);

   }
}

If you use “Input.GetKey” rather than “Input.GetKeyDown” it will fire off every frame until you let go of the key.

Depending on the length of the audio clip this will cause the clip to be played every frame and so it will be overlapping, not good!

Another solution would be to add the clip to the audioSource component and set it to loop. You can then call audio.Play(); on “GetKeyDown” and then audio.Stop(); on “GetKeyUp”

EDIT: Example:

function Update () {   

if (Input.GetKeyDown ("up"))

{

  audio.Play();

}

if (Input.GetKeyUp ("up"))

{

  audio.Stop();

}

}

Please keep in mind it is very important you add an AudioSource component with the clip already in it. Also make sure it “Loop” is checked on the AudioSource. Attach this script to the same gameObject and all should work!