- Home /
FPS, Trying to add sound to footsteps, Not working!
I am trying to add sound when the player is moving. I have a audio clip for running, and walking. How it works is if either w,a,s,or d, is held down play the clip during the duration of that time. Well when it plays it sounds like its looping(Maybe) extreamly fast. like ddddddddddddddd. Here is the script, yes i know this isn't the best way to go about this but it will do for now. What might the problem be? How can I fix this?
public class PlayerSound : MonoBehaviour {
public AudioClip walkSound;
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S)
|| Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.W))
{
audio.PlayOneShot(walkSound, 0.7f);
}
}
}
You're playing it constantly as you hold either of those keys, so it just plays it multiple times very fast, since you're using update
Answer by oatsbarley · Jan 25, 2014 at 11:31 PM
You should have a small delay between each footstep, so something like this should work:
public class PlayerSound : MonoBehaviour {
public AudioClip walkSound;
public float footstepDelay;
private float nextFootstep = 0;
void Update () {
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S)
|| Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.W)) {
nextFootstep -= Time.deltaTime;
if (nextFootstep <= 0) {
audio.PlayOneShot(walkSound, 0.7f);
nextFootstep += footStepDelay;
}
}
}
}
It just counts a timer down, then when it hits zero it resets it and plays the footstep sound.
Answer by JoshMBeyer · Jan 26, 2014 at 02:17 AM
Yeah, that helped great with the playing time. But I am having trouble with the rate which the sound is played. The clip its self is playing as if its being fastforward. How could I change the clip play speed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio Playing upon death 1 Answer
Gunshot sound not playing when handgun is fired 0 Answers
help with multiple sounds 0 Answers
Audio doesn't play 1 Answer
NGUI Volume And Quality Controle With Sliders Not working 1 Answer