• 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
0
Question by Mischa.Silden · Jun 20, 2011 at 05:39 AM · audiofadecrossfadekey press

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!

Comment
Add comment
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 Reply

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

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.

Comment
Add comment · Show 5 · 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 Mischa.Silden · Jun 20, 2011 at 06:20 AM 0
Share

Thanks! I'll try this out! I'm using unity 3.3.0f3

avatar image Mischa.Silden · Jun 20, 2011 at 07:28 AM 0
Share

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?

avatar image Dreamblur · Jun 20, 2011 at 07:44 AM 0
Share

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.

avatar image Dreamblur · Jun 20, 2011 at 07:48 AM 0
Share

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.

avatar image Mischa.Silden · Jun 20, 2011 at 08:37 AM 0
Share

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 :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Why do AudioMixerSnapshots not crossfade properly when transitioning? 0 Answers

simple question about coroutine an audio (I want to fade out music in javascript) 1 Answer

Activate sound without Pro filters 0 Answers

Fading out before load script not functioning correctly? 2 Answers

Can 2 iTween components run on the same gameobject simultaneously? 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