- Home /
 
Fade In/Out Audio After Enter Trigger
Using Unity Pro
Hey, so I search up on google for this solution and I got this script.
 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 OnTriggerStay() 
 { //replacing with "function Update" works and the audio clips are fading in and out, when starting the game
     fadeOut();
     if (audio1Volume <= 0.1) 
     {
         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;
     }
 }
 
               Fading is actually work but when you stay on trigger only. How to make fading it after I enter the trigger?
P.S I bought SoundManagerPro about 2 weeks ago and I wanted to do like this but I don't know how. If you guys know, please tell me :3
Answer by Tony_T · Apr 02, 2014 at 10:25 AM
I found a way to fade out the sound which works fine but it's not the best way probably. Here is the code:
function OnTriggerEnter () { audio.volume = 0.9;
 //Waiting time before fade the audio
 yield WaitForSeconds(1.3);
 audio.Stop();
 }
 
 function Update ()
 {
     if (audio.volume < 1)
     {
     //Fade the audio
         audio.volume -= 0.03; 
     }
 
 }
 
              Answer by fafase · Apr 02, 2014 at 10:31 AM
 private var alreadyCalled : boolean = false
 public var speed:float = 1.0f;
 
 function OnTriggerEnter(col:Collider)
 {
    // Make sure you do not start many coroutine
    if(! alreadyCalled){
       // Get your audio source and pass it
       FadeOutSound(audio);
       alreadyCalled = true;
    }
 }
 
 function FadeOutSound( audio:AudioSource)
 {
    while(audio.volume>0){
       audio.volume -= Time.deltaTime * speed;
       yield;
    }
    // You probably want to remove the object (or not) once the sound is gone
    Destroy(gameObject); 
 }
 
              Answer by darthtelle · Apr 02, 2014 at 10:37 AM
If you want the audio to begin fading continuously when you enter the trigger, create a flag or a state to update the audio once it has been trigged inside the Update function. This way once it knows the flag has been triggered it will always update.
 bool fadeAudio = false;
 
 function OnTriggerStay() 
 { 
 fadeAudio = true;
 }
 
 void Update()
 {
 if(fadeAudio)
 {
 // Fade audio here
 }
 }
 
              Your answer
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Fade in object using iTween 0 Answers
Audio Clip trouble 2 Answers
Play Audio-Clip On Ray Casting Collision 1 Answer
GUIlayout background image or color 0 Answers