Only play audio if it isn't already -- if (!audio.isPlaying) not working

Hi,

I have some code that plays a random audio clip, randomly pitch shifted when I am walking.

Unfortunately it plays a new sounds EVERY frame so I get a jittery jumpy sound instead of nice clear footsteps.

Heres the code I am using:

using UnityEngine;
using System.Collections;

public class footsteps : MonoBehaviour
{
    public AudioClip[] footStepAudioClips;
    AudioSource footStepAudioSource;
 
    float randomFootStep;
    float randomPitch;

    void Start()
    {
        footStepAudioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        // Check if we are walking
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
        {
            // Pick a random footstep sound to play
            randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
            footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];

            // Pick a random pitch to play it at
            randomPitch = Random.Range(-3, 3);
            footStepAudioSource.pitch = (int)randomPitch;

            // Play the sound
            footStepAudioSource.Play();
        }
    }
}

I have tried wrapping it in an

   if (!footStepAudioSource.isPlaying) {
            // Pick a random footstep sound to play
            randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
            footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];

            // Pick a random pitch to play it at
            randomPitch = Random.Range(-3, 3);
            footStepAudioSource.pitch = (int)randomPitch;

            // Play the sound
            footStepAudioSource.Play();
   }

But sadly it doesn’t effect anything.

Any help appreciated.

Thanks to @JoshDangIt for the timer idea, and @Naphier for the Coroutine implementation idea.

Below is the entire code as now implemented.

using UnityEngine;
using System.Collections;

public class footsteps : MonoBehaviour
{
    public AudioClip[] footStepAudioClips;
    AudioSource footStepAudioSource;
 
    float randomFootStep;
    float randomPitch;

    bool playing;

    void Start()
    {
        footStepAudioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        // Check if we are walking
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
        {
            if (playing == false)
            {
                StartCoroutine("playFootStep");
            }
        }
    }

    IEnumerator playFootStep()
    {
        playing = true;
        // Pick a random footstep sound to play
        randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
        footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];

        // Pick a random pitch to play it at
        randomPitch = Random.Range(1, 3);
        footStepAudioSource.pitch = (int)randomPitch;

        // Play the sound
        footStepAudioSource.Play();
        yield return new WaitForSeconds(footStepAudioSource.clip.length);
        playing = false;
    }

}

Don’t know why I didn’t think of this before, but a better and more realistic way of handling footsteps is to use Animation Events. https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html