- Home /
 
Proper walk sound / audio play script c#
Hello, I could use some help on this sound while walking C# script. When I walk the sound loops and overlaps (because it's in the update function). How do I make sure the sound doesn't play for an certain amount of time (say half a second?
my current code public AudioClip walkSound; void Update () { if (Input.GetButton("Vertical") || Input.GetButton("Horizontal") ){ audio.PlayOneShot(walkSound); } } }
thanks guys
Answer by robertbu · Apr 24, 2013 at 02:21 PM
One easy way would be to move your code to its own function and use InvokeRepeating(). The last parameter is how often the test will be repeated.
 void Start() {
     InvokeRepeating ("PlaySound", 0.0f, 0.5f);    
 }
     
 void PlaySound () {
     if (Input.GetButton("Vertical") || Input.GetButton("Horizontal") ){
           audio.PlayOneShot(walkSound);
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make walking sound effects? 0 Answers
Strange Bug or Code Error with footsteps? -1 Answers
Switch between two different sounds for walking and sprinting? 1 Answer