Hi,
I have some code that plays a random audio clip, randomly pitch shifted when I am walking.
Unfortunately it plays a new sounds EVERY frame so I get a jittery jumpy sound instead of nice clear footsteps.
Heres the code I am using:
using UnityEngine;
using System.Collections;
public class footsteps : MonoBehaviour
{
public AudioClip[] footStepAudioClips;
AudioSource footStepAudioSource;
float randomFootStep;
float randomPitch;
void Start()
{
footStepAudioSource = GetComponent<AudioSource>();
}
void Update()
{
// Check if we are walking
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
{
// Pick a random footstep sound to play
randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];
// Pick a random pitch to play it at
randomPitch = Random.Range(-3, 3);
footStepAudioSource.pitch = (int)randomPitch;
// Play the sound
footStepAudioSource.Play();
}
}
}
I have tried wrapping it in an
if (!footStepAudioSource.isPlaying) {
// Pick a random footstep sound to play
randomFootStep = Mathf.Floor(Random.Range(0, footStepAudioClips.Length));
footStepAudioSource.clip = footStepAudioClips [(int)randomFootStep];
// Pick a random pitch to play it at
randomPitch = Random.Range(-3, 3);
footStepAudioSource.pitch = (int)randomPitch;
// Play the sound
footStepAudioSource.Play();
}
But sadly it doesn’t effect anything.
Any help appreciated.