I’m playing around with the footstep audio in my game to try to get things sounding a little more realistic. I’ve started out with what I thought was a pretty simple idea, but it isn’t working and I’m stumped as to why.
I have this Update function:
void Update()
{
if (isMoving)
{
if (!audio.isPlaying)
{
int footstepSound = UnityEngine.Random.Range(1, 5);
Debug.Log(footstepSound);
switch (footstepSound)
{
case 1:
audio.PlayOneShot(soundFootstep1);
break;
case 2:
audio.PlayOneShot(soundFootstep2);
break;
case 3:
audio.PlayOneShot(soundFootstep3);
break;
case 4:
audio.PlayOneShot(soundFootstep4);
break;
}
}
}
}
And this portion of code comes from one of the movement functions:
What’s happening now is that the audio plays, but it’s basically hundreds of the sounds all playing at once.
What I’m expecting to happen is that it will play the random footstep sounds one after another.
For example, say the total movement action takes 3 seconds, and each sound is 1 second long. Shouldn’t it start playing the first sound, causing isPlaying to be set to true for the next 1 second. Then play the next sound, again “blocking” further sounds for 1 second. And then finally, play one last sound in the third second?
It seems like the way it’s working at the moment, isPlaying is always false regardless of whether or not there is a sound playing.
I suggest to time your step sound, using a global variable that holds the time left till the end of audio clip played. Decrease this variable in Update with Time.deltaTime and only initiate another random sound when this variable is zero or less than zero. Then reinitialize this variable with audio clip length.
steveJ try playing only one sound, and to make it sound differently add the “Random” and the “Pitch” with a range od (-2, 2). so when you take steps the sound will be different in pitch, sometimes high or sometimes low. so you could use one sound
I dont think so, had the same problem in one of my games. I switched to using audio.Play() - it will stop anyways. And then i put it into a coroutine with a timer based on my running speed. Worked pretty great.