- Home /
audio troubles changing from one to another (stop it consistently changing)
so i have this code and the clip keeps changing , basically i need it to finish playing the clip until the person stops moving forward and starts being idle at which point it needs to change to the idle sound effects without constantly changing the idle sound effect(just to be clear both idle sound effect clips and walking effect clips keep changing so i get literally a frames sound from the clip before it changes to another)
they also need to loop.
thanks for any help in advance ! using UnityEngine;
using System.Collections;
public class fxs : MonoBehaviour {
public GameObject player;
public bool reset = false;
public AudioClip[] walk;
public AudioClip[] idle;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
PickFx();
}
void PickFx() {
TP_Animator other = player.GetComponent<TP_Animator>();
if(other.walking == true){
//problem is here
audio.clip = walk[Random.Range(0, walk.Length)];
audio.Play();
}
else if(other.walking == false)
{
audio.clip = idle[Random.Range(0, idle.Length)];
audio.Play();
}
}
}
it changes constantly because walking is constantly returning true so i keep getting it changing clip same with if false(idle sounds) im aware i put pickfx in update but i dont know where else to put it if there is one.
Answer by aldonaletto · May 18, 2012 at 11:48 AM
You could either wait for the current sound to end before playing a new one, or wait for some delay. Let's use the delay in the walking state, and the other alternative in the idle case:
float stepTime = 0; float stepInterval = 0.3f;
void PickFx() { TP_Animator other = player.GetComponent(); if(other.walking == true){ // if walking... if (Time.time >= stepTime){ // and stepTime has come... audio.clip = walk[Random.Range(0, walk.Length)]; audio.Play(); // play the walk sound... stepTime = Time.time + stepInterval; // and set new stepTime } } else // if not walking... { if (audio.isPlaying == false){ // and previous sound has finished... audio.clip = idle[Random.Range(0, idle.Length)]; audio.Play(); // play random idle sound } } } The interval approach is better for footstep sounds - you can even change stepInterval according to the character speed - while the other alternative is better for generating a continuous random sequence of sounds.
works great thank you . i think i might need to do some work on my step sound to make it fit better but it works! thanks
You can use isPlaying in both cases, depending on how your walk sounds are (a worm crawling, for instance) - but for footsteps the delay approach is the best choice.
Your answer
Follow this Question
Related Questions
New Input system holding down a button 2 Answers
Audio Script 1 Answer
Slow motion all but one - yet another round, hopefully the last 6 Answers
Making sounds wait for each other to finish 2 Answers
Slow update best practice 0 Answers