- Home /
Change music during chase
I'm trying to make the music change whenever the player is being chased by "Demon". The music being played normally is "CreepyMusic" and the chase one is called "HorrorChase". Both are attached as 2D sounds to the player alongside an audio listener but I have no idea of how to alternate them. How could I do this?
Answer by rutter · Jul 14, 2014 at 12:46 AM
Assuming you have a reference to each AudioSource (possibly set from the inspector), you can tell one to Stop and the other to Play:
//two AudioSource variables
defaultMusic.Stop();
chaseMusic.Play();
If you want to be fancy about it, you could "fade out" one track, or "fade in" the other, by setting their volume over multiple frames. Tweening libraries such as iTween can be very helpful for that.
So far I have this:
#pragma strict
var default$$anonymous$$usic : AudioClip;
var chase$$anonymous$$usic: AudioClip;
function OnTriggerEnter() {
default$$anonymous$$usic.Stop();
chase$$anonymous$$usic.Play();
}
function OnTriggerExit() {
chase$$anonymous$$usic.Stop();
default$$anonymous$$usic.Play();
}
But it says that 'Stop' and 'Play' are not members of 'UnityEngine.AudioClip'.
and that's because they aren't, you should use 2 AudioSources with the clips attached to them (or use one and change its clip value, (but if you use 2 you can fade out one and fade in the other one ins$$anonymous$$d of abruptly cutting them off)
Your answer
Follow this Question
Related Questions
Stereo Mix as Input? 1 Answer
Can not play a disabled audio source 2 Answers
Multi Channel Audio 4 Answers
How do I only start the audio when player is at max distance 1 Answer
Audio from new scene begins half way through loading. 2 Answers