Fade in/out music script

I’m trying to write a fade in /out script for some music. However, it gets quieter (but not as much as it should do) but then when the volume is meant to be increasing, it stays steady. What am I doing wrong?
var track1 : AudioClip;
var track2 : AudioClip;
var trackVolume = 5.0;

audio.clip = track1;
audio.Play();

function Update () {
if (Input.GetButtonDown ("Jump")){
fadeOut();

}
if (audio.volume <=0.1){
audio.clip = track2;
fadeIn();

}
}


function fadeIn(){
if (audio.volume < 1){
audio.volume += 1 * Time.deltaTime;
print (audio.volume + "goingin");
}
}

function fadeOut(){
audio.volume -= 1 * Time.deltaTime;
print (audio.volume);
}

I had fun playing around with this one :slight_smile:
You need to remove the line that calls for getting the Jump button, as this is only being called once. If you want to press the jump button to start the fade, then I suggest creating a variable called fadeOutStarted, and set that to true, then in your update method keep executing fadeout() if that var has been set to true.

I have modified the script, the reason why it wasn’t fading back in was it kept trying to play track2 from the beginning. So I created a ‘track2Playing’ variable, then if we know track is playing = true, we don’t need to call audio.play() anymore, and just carry on with the fade in.

var track1 : AudioClip;
var track2 : AudioClip;

audio.clip = track1;
audio.Play();

var audio1Volume : float = 1.0;
var audio2Volume : float = 0.0;
var track2Playing : boolean = false;

function Update() {
    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;
    }
}

The way you have it set up, it’s going to return true the first frame “Jump” is fired, and only subtract an extremely small amount from volume. Then in the second if block, you are checking if the volume is less than 10% and adding another extremely small amount to it and exiting.

You should check out coroutines, here is an example of the fade in function as a coroutine

function fadeIn()
{
    var t = 0.0;
    while (t < 1.0) {
        t += Time.deltaTime;
        audio.volume = t;
        yield;
    }
}

You need to keep updating your fade. You can keep a fade speed as a variable to keep track of the current fade operation. -1 being a fadeout and 1 is fadein
Sorry for the crappy formating, hope that sends you in the righ direction, should work.

audio.clip = track1;
audio.Play();

private var fadeSpeed:float = 0;

function Update () {

if (Input.GetButtonDown ("Jump")){
fadeOut();
}

// Update the fade.
if (fadeSpeed < 0)
{
audio.volume = Mathf.Max(audio.volume + fadeSpeed * Time.deltaTime, 0);
print (audio.volume);
// Done fading out.
if(audio.volume == 0)
{
audio.clip = track2;
fadeIn();
}
}
else if (fadeSpeed > 0)
{
audio.volume = Mathf.Min(audio.volume + fadeSpeed * Time.deltaTime, 1);
print (audio.volume);
// Done fading in.
if(audio.volume == 1)
{
fadeSpeed = 0;
}
}
}

function fadeIn(){
if (audio.volume < 1){
fadeSpeed = 1;
}
}

function fadeOut(){
if(audio.volume > 0)
{
    fadeSpeed = -1;
}
}

Here is how I did it with good results. Just place trigger collider spheres (I call them audio zones in the game) in specific areas.
If it is helpful for someone please go ahead and use the snippet.

void OnTriggerEnter(Collider other)

{
	theCollider = other.tag;

	if (theCollider == "Player") 
	{
		StartCoroutine(fadeIn());
	}
}

void OnTriggerExit(Collider other)
{
	theCollider = other.tag;

	if (theCollider == "Player") 
	{
		StartCoroutine(fadeOut());
	}
}

IEnumerator fadeIn()
{
	AmbientAudio.volume = 0.0f;
	AmbientAudio.Play ();
	AmbientAudio.loop = true;

	float t = 0.0f;
	while (t < MaxVol) {
		t += Time.deltaTime;
		AmbientAudio.volume = t;
		yield return new WaitForSeconds(0);
	}
}

IEnumerator fadeOut()
{
	float t = MaxVol;
	while (t > 0.0f) {
		t -= Time.deltaTime;
		AmbientAudio.volume = t;
		yield return new WaitForSeconds(0);
	}

	AmbientAudio.volume = 0.0f;
	AmbientAudio.Stop ();
	AmbientAudio.loop = false;
}

You have a couple things wrong:

1.You are using GetButtonDown, which only registers the one frame that you button FIRST down so fade out will only be called once unless you keep jamming that button.

2.When you are fading out, you stop at .1 volume, which is not 0–which is why it’s not silent when it’s about to fading back in.
3. Every frame you are checking if the volume is less than .1–which it will only be ONCE when it fades out far enough. Ao its going to only run fadeIn() once, maybe twice. That isn’t much of a volume change so it will just appear to remain at the same volume.

You need to have if statements test against 0 and 1.

You need to either:

-have fadeIn and fadeOut only be called once. Within those functions, have while loops that handle decreasing/increasing the volume.

-have booleans for fading in and fading out. In you update loop, use those to decrease or increase the volume

Right now you’re doing a mixture of both, and it’s not working.
OR just try SoundManagerPro

https://forum.unity3d.com/threads/audiosource-cross-fade-component.443257/

This is my simplified class:

using System.Collections;
using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class SoundEffect : MonoBehaviour
{
    private AudioSource source;

    private void Awake()
    {
        source = gameObject.GetComponent<AudioSource>();
    }

    public void PlayWithFade()
    {
        if (source == null)
        {
            Debug.LogError("Can't play sound!");
            return;
        }

        StartCoroutine(FadeIn(3f, 0, 0.8f));
    }

    public void StopWithFade()
    {
        if (source == null)
        {
            return;
        }

        StartCoroutine(FadeOut(3f));
    }

    private IEnumerator FadeIn(float fade_time, float start_volume, float finish_volume)
    {
        source.volume = start_volume;
        source.Play();

        while (source.volume < finish_volume)
        {
            source.volume += finish_volume * Time.deltaTime / fade_time;
            yield return null;
        }

        source.volume = finish_volume;
    }

    private IEnumerator FadeOut(float fade_time)
    {
        float start_volume = source.volume;

        while (source.volume > 0)
        {
            source.volume -= start_volume * Time.deltaTime / fade_time;
            yield return null;
        }

        source.Stop();
        source.volume = start_volume;
    }
}

THERE IS A SIMPLER WAY TO DO THIS!

Just use a for loop. (;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FadeOutScript: MonoBehaviour
{

    AudioSource yourAudio;

    IEnumerator FadeOut()
    {
        for (int i = 1; i <= 10; i++)
        {
            yourAudio.volume -= 0.1f;
            yield return new WaitForSecondsRealtime(0.1f);
        }
    }

    void Start()
    {
        yourAudio.GetComponent<AudioSource>();
    }

    void Update ()
    {
        if (/* the thing that triggers the fading audio */) StartCoroutine(FadeOut());  
	}
}