- Home /
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);
}
There's never a point in multiplying by 1. http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
I sometimes do multiply by 1 when it's a factor or a speed that could change later, I put 1 and later it can become 1.2 or whatever, you know, it's just clearer. Just to keep in $$anonymous$$d it's a factor. So his 1 * doesn't bother me at all.
I did it in case I wanted to change it later - faster/slower fading. Anyway, does anyone have an answer?
if you know it can change later why not put it into a variable
Answer by Meltdown · May 23, 2011 at 09:45 PM
I had fun playing around with this one :) 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;
}
}
great to see we all have our ways haha I do find pyro's yield method intriguing I'm just totally not used to yield logics.
Did you set your audiosource1 starting volume to 1, and your audiosource2 to volume 0 in the inspector?
Ah...I was doing it differently to you. I use ONE audio source, which I get to change tracks, so only had the volume at 1.
Doesn't make a difference I guess, you'd just need to change the code slightly :p
Answer by pyro · May 23, 2011 at 08:58 PM
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;
}
}
Answer by Evil-Dog · May 23, 2011 at 08:56 PM
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;
}
}
Answer by sidhants1 · Mar 04, 2016 at 08:11 PM
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;
}
Answer by madflyskills · Jun 13, 2013 at 12:07 AM
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
Your answer
Follow this Question
Related Questions
Fade multiple GuiTextures in ONE Javascript 1 Answer
Converting LoadLevelFade to C# 2 Answers
Script not working in Unity 4 2 Answers
Play and stop sound 1 Answer
Playing Music Problem. 2 Answers