- Home /
Question by
SwiftKraft · Oct 28, 2020 at 11:25 PM ·
c#audiosoundfootsteps
Strange Bug or Code Error with footsteps?
I am currently working on a first person game, and I searched how to do footsteps and followed a tutorial, but the footsteps currently plays only when I walk down slopes and at random points.
Here is the code for the footsteps
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.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;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
play audio for few seconds on key hit 1 Answer
Audio listener to mono then to left or right speaker? 2 Answers
Trigger audio loop on beat with PlayScheduled 1 Answer
Play audio from directory? 0 Answers