- Home /
Audio fade after entering trigger?
I want to fade out one audio clip and fade in a different audio clip when entering a trigger. The code seems to work, when i replace function OnTriggerEnter with function Update. The audio clips are fading in and out, when starting the game. Any ideas? Thanks in advance.
var track1 : AudioClip;
var track2 : AudioClip;
function Start(){
audio.clip = track1;
audio.Play();
}
var audio1Volume : float = 1.0;
var audio2Volume : float = 0.0;
var track2Playing : boolean = false;
function OnTriggerEnter() { //replacing with "function Update" works and the audio clips are fading in and out, when starting the game
fadeOut();
if (audio1Volume <= 0.4) {
if(track2Playing == false)
{
track2Playing = true;
audio.clip = track2;
audio.Play();
}
fadeIn();
}
}
function OnGUI() { GUI.Label(new Rect(10, 10, 200, 100), "Audio 1 : " + audio1Volume.ToString());
GUI.Label(new Rect(10, 30, 200, 100), "Audio 2 : " + audio2Volume.ToString());
}
function fadeIn() {
if (audio2Volume < 1) {
audio2Volume += 0.1 * Time.deltaTime;
audio.volume = audio2Volume;
}
}
function fadeOut() {
if(audio1Volume > 0.1)
{
audio1Volume -= 0.1 * Time.deltaTime;
audio.volume = audio1Volume;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613105146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio Trigger Problems 1 Answer
Audio Clip constantly play 1 Answer
OnTriggerEnter2D --> One Shot Audio Problem 1 Answer
Wanting audio to pause when entering a trigger zone 1 Answer
OnTriggerExit stop audio 1 Answer