- Home /
Audio crossfade on single key press
Hi! I'm trying to crossfade two audio clips on one key press but I just can't wrap my head around it.
I have a game object with two children (both have an audio source). The current script that I'm using works but not as intended.
using UnityEngine;
using System.Collections;
public class scr_MusicController : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("n"))
{
transform.FindChild("aud_Normal").audio.volume = 1;
transform.FindChild("aud_8bit").audio.volume = 0;
}
if (Input.GetKeyDown("m"))
{
transform.FindChild("aud_Normal").audio.volume = 0;
transform.FindChild("aud_8bit").audio.volume = 1;
}
}
}
I want to assign this cycle to single key and the volume increase to take one second to reach it's assigned value (as well on the volume decrease of course).
I know this is a lot to ask but please, I'm stuck in here :/
Thanks for your help!
Answer by Dreamblur · Jun 20, 2011 at 06:06 AM
private IEnumerator ChangeMusic()
{
float fTimeCounter = 0f;
while(!(Mathf.Approximately(fTimeCounter, 1f)))
{
fTimeCounter = Mathf.Clamp01(fTimeCounter + Time.deltaTime);
audio1.volume = 1f - fTimeCounter;
audio2.volume = fTimeCounter;
yield return new WaitForSeconds(0.02f);
}
StopCoroutine("ChangeMusic");
}
audio1 is the AudioSource that you're fading out while audio2 is the AudioSource that you're fading in. Just modify the coroutine so that the variables correspond to what you need you actually need to alter.
By the way, what version of Unity are you using? I've never encountered FindChild before.
Hi again! I tried your script but it does nothing.
Sure it plays the both songs at the same time but no volume changes occur.
This is how the code looks like now.
using UnityEngine;
using System.Collections;
public class scr_$$anonymous$$usicController : $$anonymous$$onoBehaviour
{
public AudioSource Normal;
public AudioSource Retro;
private IEnumerator Change$$anonymous$$usic()
{
float fTimeCounter = 0f;
while (!($$anonymous$$athf.Approximately(fTimeCounter, 1f)))
{
fTimeCounter = $$anonymous$$athf.Clamp01(fTimeCounter + Time.deltaTime);
Normal.volume = 1f - fTimeCounter;
Retro.volume = fTimeCounter;
yield return new WaitForSeconds(0.02f);
}
StopCoroutine("Change$$anonymous$$usic");
if (Input.Get$$anonymous$$eyDown("m"))
StartCoroutine("Change$$anonymous$$usic");
}
}
What am I doing wrong?
You were supposed to keep the code you had from before and just add the IEnumerator I provided with your own modifications. Basically, you would use StartCoroutine to call the IEnumerator Change$$anonymous$$usic() when the proper input was received from inside Update().
Basically, just replace this block of code from your original script:
if (Input.Get$$anonymous$$eyDown("n"))
{
transform.FindChild("aud_Normal").audio.volume = 1;
transform.FindChild("aud_8bit").audio.volume = 0;
}
with this:
if (Input.Get$$anonymous$$eyDown("m"))
{
StartCoroutine("Change$$anonymous$$usic1");
}
where Change$$anonymous$$usic1 is a modified version of the code I posted (replacing audio1 with the fading-out music and audio2 with the fading-in music).
This block of code:
if (Input.Get$$anonymous$$eyDown("n"))
{
transform.FindChild("aud_Normal").audio.volume = 1;
transform.FindChild("aud_8bit").audio.volume = 0;
}
would then be replaced by this:
if (Input.Get$$anonymous$$eyDown("m"))
{
StartCoroutine("Change$$anonymous$$usic2");
}
where Change$$anonymous$$usic2 is a modified version of the code reversing the 2 audio sources.
By the way, you can choose to use one IEnumerator for both music transition processes by passing a parameter to the original IEnumerator Change$$anonymous$$usic() which would deter$$anonymous$$e what the fading-out and fading-in music would be. If you want to do something like that ins$$anonymous$$d of using 2 IEnumerators, then just specify a parameter and use an if-else or a switch-case function to iterate through your music combinations.
Thanks a ton! I works! Now I just need to figure out how to assign them to the same key and let them cycle each time the specified key is pressed :)