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.