Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Muzz5 · May 23, 2011 at 08:23 PM · javascriptmusicfade

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);
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · May 23, 2011 at 08:30 PM 0
Share

There's never a point in multiplying by 1. http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

avatar image Evil-Dog · May 23, 2011 at 08:39 PM 0
Share

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.

avatar image Muzz5 · May 23, 2011 at 08:51 PM 0
Share

I did it in case I wanted to change it later - faster/slower fading. Anyway, does anyone have an answer?

avatar image pyro · May 23, 2011 at 08:53 PM 3
Share

if you know it can change later why not put it into a variable

8 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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;
     }
 }
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Evil-Dog · May 23, 2011 at 10:28 PM 0
Share

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.

avatar image Muzz5 · May 24, 2011 at 12:12 PM 0
Share

Doesn't fade back in...just nicely fades out!

avatar image Meltdown · May 24, 2011 at 01:15 PM 0
Share

Did you set your audiosource1 starting volume to 1, and your audiosource2 to volume 0 in the inspector?

avatar image Muzz5 · May 24, 2011 at 01:38 PM 0
Share

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.

avatar image Meltdown · May 24, 2011 at 01:46 PM 0
Share

Doesn't make a difference I guess, you'd just need to change the code slightly :p

Show more comments
avatar image
4

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
 }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Muzz5 · May 24, 2011 at 12:14 PM 0
Share

Doesn't fade back in afterwards.

avatar image
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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges