I have 3 audio clips: startSource with 10s length; loopSource that is a loop of engine and crashSource that is the sound of collision with 2s.
I have one script that should play the audio clip with loop (loopSource) after the first audio clip (startSource) is finished and another audio clip for playing the crashSource sound on trigger event.
If I let length startSource playing until the end, everything works fine.
When I stop the startSource before the end of the length,by the trigger event, the rest of the startSource length and the loopSource clip starts to play in the background, as if the startSource was paused or "WaitForSeconds " did not stop counting when I stop all the audiosources.
Here is my code from first script:
public AudioClip startClip;
public static AudioSource startSource;
public AudioClip loopClip;
public static AudioSource loopSource;
public AudioClip crashClip;
public static AudioSource crashSource;
private void Start ()
{
startSource = gameObject.AddComponent<AudioSource>();
startSource.playOnAwake = false;
startSource.rolloffMode = AudioRolloffMode.Linear;
startSource.loop = false;
startSource.clip = startClip;
loopSource = gameObject.AddComponent<AudioSource>();
loopSource.playOnAwake = false;
loopSource.rolloffMode = AudioRolloffMode.Linear;
loopSource.loop = true;
loopSource.clip = loopClip;
crashSource = gameObject.AddComponent<AudioSource>();
crashSource.playOnAwake = false;
crashSource.rolloffMode = AudioRolloffMode.Linear;
crashSource.clip = crashClip;
if(soundOn){
StartCoroutine(StartSounds());
}
}
void OnTriggerEnter2D(Collider2D other){
if(GameScreen.gameStart){
GameScreen.gameStart = false; // game will stop
if(MenuScreen.soundOn){
crashSource.Play(); // play crash sound
}
startSource.Stop(); // stop engine sound
loopSource.Stop(); // stop engine sound
StartCoroutine(ShowGameStop());
}
}
private IEnumerator ShowGameStop() {
yield return new WaitForSeconds (3.0f);
GameScreen.gameStart = true;
bang.SetActive(false);
playerCar.rectTransform.anchoredPosition = new Vector3 (0, playerPos, 0);
StartCoroutine (StartSounds ());
}
private IEnumerator StartSounds() {
startSource.Play();
yield return new WaitForSeconds (startSource.clip.length);
startSource.Stop();
loopSource.Play();
}
The audio file for startSource is mp3 and the loop audio is ogg. Is this a problem ?
All the help is welcome.