Footsteps? Can someone help me plz.

I am currently working on a FPS game, I got stuck in a problem: Footsteps only playing at random points.
Here is my footsteps code:

Footsteps.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class Footsteps : MonoBehaviour
{

    private AudioSource sound;

    public AudioClip[] soundlist;

    private CharacterController character;

    public float volume_Min, volume_Max;
    public float stepDist;

    private float dist;

    void Awake()
    {
        sound = GetComponent<AudioSource>();

        character = GetComponentInParent<CharacterController>();
    }

    void Update()
    {
        PlaySound();
    }

    void PlaySound()
    {
        if (character.isGrounded == false)
            return;

        if (character.velocity.magnitude > 0f)
        {
            dist += Time.deltaTime;

            if(dist >= stepDist)
            {
                sound.volume = Random.Range(volume_Min, volume_Max);
                sound.clip = soundlist[Random.Range(0, soundlist.Length)];
                sound.Play();

                dist = 0f;
            }
        }
    }
}

The code does not have any errors whatsoever.

Glancing over what you have above seems reasonable, so it’s likely some other kind of setup or config error.

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

NOTE: The above code does not play footsteps any faster if you move faster. It plays footsteps at the stepDist interval and it advances time anytime the movement magnitude is even slightly nonzero.

This seems not right. Usually you would multiply the rate of time advance by the speed of motion, and do away with the if check on line 37 above.

My movement script adjust the StepDist value when it sprints or crouches.