- Home /
Only play audio if it isn't already -- if (!audio.isPlaying) not working
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.
Not sure why audio.isPlaying isn't working. Have you tried adding a timer to see if the time since it last played a footstep is greater than footStepAudioSource.clip.length?
Will have a go at that. Just need to work out how :)
Im not sure why, but sometimes relying on ! doesnt always work, but printing out the full thing footStepAudioSource.isPlaying == false
works sometimes, and the same with bool conditions - I have no idea, but that often solves most issues I ever have with those issues.
Hi thanks for that, I changed it but still no effect
That's not true at all. If this has worked for you then you're doing something else wrong.
What part of the code are you wrapping? If you do it after you set the clip then that might be the problem.
edited question to reflect this, but i wrapped everything inside the movement check.
AudioSource.isPlaying should absolutely work. If not, maybe it is some editor glitch or bug. Test it out to doubly make sure. Add a public bool to your class and every frame set that = to footStepAudioSource.isPlaying then watch it in the editor. There can be issues if you use PlayOneShot(), but you're not. Alternatively, use a coroutine with a timer. Just yield return new WaitfForSeconds(footStepAudioSource.clip.length). Use a bool to see if the Coroutine is running or has finished.
Answer by BeakerTeck · Oct 20, 2016 at 08:01 AM
Thanks to @JoshDangIt for the timer idea, and @Naphier for the Coroutine implementation idea.
Below is the entire code as now implemented.
using UnityEngine;
using System.Collections;
public class footsteps : MonoBehaviour
{
public AudioClip[] footStepAudioClips;
AudioSource footStepAudioSource;
float randomFootStep;
float randomPitch;
bool playing;
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))
{
if (playing == false)
{
StartCoroutine("playFootStep");
}
}
}
IEnumerator playFootStep()
{
playing = true;
// 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(1, 3);
footStepAudioSource.pitch = (int)randomPitch;
// Play the sound
footStepAudioSource.Play();
yield return new WaitForSeconds(footStepAudioSource.clip.length);
playing = false;
}
}
Answer by JoshDangIt · Oct 20, 2016 at 04:22 PM
Don't know why I didn't think of this before, but a better and more realistic way of handling footsteps is to use Animation Events. https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
AudioSource.GetSpectrumData Failure 0 Answers
Wwise Integration: Where do i start Coding? 1 Answer
How can I play a sound every time a particle is emitted? 3 Answers