- Home /
Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly
Hello eveyone, i want to make a footstep sound but my code doesn't make the audio work properly It repeats itself for like infiite times when i press the "W" button. Here is my code:
private void PlayFootStepAudio()
{
if (!m_CharacterController.isGrounded)
{
return;
}
// pick & play a random footstep sound from the array,
// excluding sound at index 0
int n = Random.Range(1, m_FootstepSounds.Length);
m_AudioSource.clip = m_FootstepSounds[n];
m_AudioSource.PlayOneShot(m_AudioSource.clip);
// move picked sound to index 0 so it's not picked next time
m_FootstepSounds[n] = m_FootstepSounds[0];
m_FootstepSounds[0] = m_AudioSource.clip;
}
How can i a make this work properly?
Kind Regards, Cesurix
you didn't provide all the code, but I assume you are using something like input keyDown so that if the key is held down the sound will start playing every frame.. you could do a check to see if the audio clip is already being played before you play it, although then you can no overlap the sound. At any rate I would recommend using a sound manager, there are some really good free ones on the asset store.
Answer by pauldarius98 · Feb 24, 2021 at 08:50 PM
Thr problem is that most likely you run PlayFootStepAudio() in update and so as long as the character is on the ground, the audio source will play a footstep sound each frame. It depends on the game but if you have animation to the character, you could add 2 events on the animation (1 for each foot) to trigger the PlayFootStepAudio function and if not, you could run this code with a given delay between each step. I will attach the code for the second case
private float lastPlayTime = 0f;
private float delayBetweenSteps = 0.5f;
private void PlayFootStepAudio()
{
if (!m_CharacterController.isGrounded || Time.time - lastPlayTime <= delayBetweenSteps)
{
return;
}
lastPlayTime = Time.time
// pick & play a random footstep sound from the array,
// excluding sound at index 0
int n = Random.Range(1, m_FootstepSounds.Length);
m_AudioSource.clip = m_FootstepSounds[n];
m_AudioSource.PlayOneShot(m_AudioSource.clip);
// move picked sound to index 0 so it's not picked next time
m_FootstepSounds[n] = m_FootstepSounds[0];
m_FootstepSounds[0] = m_AudioSource.clip;
}
I tried your script and now my code works properly but i can't hear any footstep sounds:( The delay between two soundtrack works. Is there a problem with the Audio Source or do i make something silly? :) I can't figure out what is the problem. Is there anyone who can help me?
Sorry, I have not taken into account the random audios.
There can be a lot of cases. Make sure that you have an audio listener (usually attached to the current camera) And check the volume and the pitch of the audio source, where the audio source is positioned (should be on player), Volume Rolloff. If none of this works (aka are properly set) please provide more information with your audio setup (audio source, audio listener, etc)
Thank you very much. I made something silly, the pitch was on -0.80. Thanks again for your kind answer:) Kind Regards, Cesurix
Answer by Jinkel92 · Feb 25, 2021 at 11:43 AM
Well, you could change this:
m_AudioSource.clip = m_FootstepSounds[n];
m_AudioSource.PlayOneShot(m_AudioSource.clip);
For this:
m_AudioSource.PlayOneShot(m_FootstepSounds[n].clip);
Thank you for trying to help me:) The problem was with the pitch in AudioSource. Kind Reagrds, Cesurix