Footsteps Problem

Hello , i want to add footsteps sound to player

	public float walkSpeed = 6.0f;
	public float runSpeed = 10.0f;
	public AudioClip[] dirt ; 

void Update () {
	
	if (!grounded && walkSpeed > 4) {
			audio.clip = dirt[Random.Range(0, dirt.Length)];
			audio.Play();

		}

but sound only playing when i jump it’s not playing while im moving any help would be great.

Ok… So Add this to your variables:

public float SoundFrequency = 1.0f;

Add this on Start:

StartCoroutine(PlaySound());

Then remove your condition and the code from your Update Method.

And change the PlaySound method to:

private IEnumerator PlaySound()
{
    while (true)
    {
        if (grounded && speed > 4)
        {
            audio.clip = dirt[Random.Range(0, dirt.Length)];
            audio.Play();
            yield return new WaitForSeconds(SoundFrequency);
        }
        else
        {
            yield return null;
        }
    }
}

The only thing you need to change is that speed is NEVER < 4. So even when you are not moving the sounds are still played.

Should it not be

if (grounded && walkSpeed > 4) {