How can I stop playing the AudioSource with a fade? From volume 1.0 to 0?
var audioSource : AudioSource;
function FixedUpdate() {
if(Input.GetKey("f")) {
audioSource.Stop();
}
}
How can I stop playing the AudioSource with a fade? From volume 1.0 to 0?
var audioSource : AudioSource;
function FixedUpdate() {
if(Input.GetKey("f")) {
audioSource.Stop();
}
}
private var FADE_HOW_MANY_SECONDS = 3.0;
function fadeTitlesMusicNow()
{
var volNow:float = menuMusic.volume;
while ( volNow > 0 )
{
volNow -=
( Time.deltaTime
* MUSICVOL
/ FADE_HOW_MANY_SECONDS );
if ( volNow <= 0.0 ) volNow = 0.0;
menuMusic.volume = volNow;
yield;
}
menuMusic.Stop();
}
Note: in the real world it’s harder than this. You’d have to ensure locking (and other problems I’m too drunk to address.) For example, “you” may accidentally, as it were, call this routine more than once while it is already running. (This is a very common issue in “wrap up the scene” -type of code.) So again in the real world it would be more complicated than this. But that’s a simple example of using yield to fade volume.