0
\$\begingroup\$
        if (Input.GetKey(KeyCode.A))
        {
            audioSource.Play();
        }

Except this only functions once as the key is held down. Same goes with GetKeyDown. Is there any way I can get this to loop as long as they key is being held down? (Is in Update)

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Input.GetKeyDown is true on the frame when the player started pressing the key and Input.GetKeyUp on the frame when it is released. So all you need to do is call audioSource.Play on GetKeyDown and then AudioSource.Stop on GetKeyUp:

if (Input.GetKeyDown(KeyCode.A)){
    audioSource.Play();
}
if (Input.GetKeyUp(KeyCode.A)){
    audioSource.Stop();
}

Note that Stop will reset the audio clip to the beginning. There are also cases where you just want to pause the audio clip and then later resume it later at the same point. In that case use Pause and UnPause instead of Stop and Play.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .