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
0
Question by IvyHorse · Aug 24, 2015 at 11:27 AM · c#coroutineaudiosourceaudioclipfade out

Fade, Then Stop all other audiosources attached to gameobject?

I have a script that has 12 audio sources and 12 audio clips attached to object1 , each audio source is in charge of playing one clip. which clip is played depends upon the animation state of object2 when collided with. The problem is with lots of object2's colliding into object1, lots of clips are being played that all overlap, it doesn't sound good. my intention is that when object3 collides with object1, hopefully slightly before object2 does. The current audio source, or rather all audio sources playing fade and then stop before object2 collides, thus removing the overlap. I thought perhaps a coroutine would be a good start, but not sure how to implement it. Help appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class InstrumentController : MonoBehaviour {
     
     public AudioSource[] audios;
 
     private AudioSource sourceA;
     private AudioSource sourceASharp;
     private AudioSource sourceB;
     private AudioSource sourceC;
     private AudioSource sourceCSharp;
     private AudioSource sourceD;
     private AudioSource sourceDSharp;
     private AudioSource sourceE;
     private AudioSource sourceF;
     private AudioSource sourceFSharp;
     private AudioSource sourceG;
     private AudioSource sourceGSharp;
 
 
 
     void Start()
     {
 
         audios = GetComponents<AudioSource> ();
         
         sourceA = audios [0];
         sourceASharp = audios [1];
         sourceB = audios [2];
         sourceC = audios [3];
         sourceCSharp = audios [4];
         sourceD = audios [5];
         sourceDSharp = audios [6];
         sourceE = audios [7];
         sourceF = audios [8];
         sourceFSharp = audios [9];
         sourceG = audios [10];
         sourceGSharp = audios [11];
 
         
     }

     IEnumerator FadeAudio ()
     {
 
     }
     
     
 
 
     void OnTriggerEnter2D(Collider2D col)
     {
         //object2 collisions
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Rednote"))
         {
             sourceB.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Bluenote"))
         {
             sourceC.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Bluepurplenote"))
         {
             sourceCSharp.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Purplenote"))
         {
             sourceD.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greypurplenote"))
         {
             sourceDSharp.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greynote"))
         {
             sourceE.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Yellownote"))
         {
             sourceF.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Yellowgreennote"))
         {
             sourceFSharp.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greennote"))
         {
             sourceG.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greenorangenote"))
         {
             sourceGSharp.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Orangenote"))
         {
             sourceA.Play();
         }
         if (col.gameObject.tag == "notecollider" && col.GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Pinkorangenote"))
         {
             sourceASharp.Play();
         }
 
 
 }
 }
 
 
         //object3 collisions
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Rednote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Bluenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Bluepurplenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Purplenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greypurplenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greynote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Yellownote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Yellowgreennote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greennote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Greenorangenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Orangenote"))
         {
             StartCoroutine(FadeAudio());
         }
         if (col.gameObject.tag == "note" && col.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName ("Pinkorangenote"))
         {
             StartCoroutine(FadeAudio());
         }
 
     }
 }
 






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
0
Best Answer

Answer by Priyanshu · Aug 24, 2015 at 05:23 PM

  1. Incase you don't need fade effect.

    One quick solution is attach single audiosource. Everytime you collide, assign the particular audioClip to the audioSource and then play Audio.

  2. Otherwise if you want fade effect. You'll need AudioMixer. Here a decent Tutorial.

Comment
Add comment · Show 2 · 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 IvyHorse · Aug 24, 2015 at 06:26 PM 0
Share

That's a good idea, I'll try it. The problem I've been experiencing with methods similar to that though is that when the audio is stopped immediately, there is a loud clipping sound. I'll try this though and see what happens, thanks!

Yeah so I tried it, and it works except as I feared there's a loud clipping sound when it changes, my thought was that if I could fade the volume before it changed perhaps I wouldn't be able to hear the clipping sound.

avatar image Priyanshu · Aug 25, 2015 at 05:40 AM 0
Share

Hmm maybe try using Audio$$anonymous$$ixer then. If you want to fadeaway all the other sounds. Follow the tutorial i posted above. If u have any problem then, feel free to ask.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio playing not in right time when calling from script 1 Answer

I have a shooting script. I have a problem: when firing bursts, my sound of a shot breaks. How to make the sound produced to the end. 0 Answers

How to play a sound when a prefab button that changes scene is clicked 3 Answers

Stop audioclip from another script 1 Answer

Triggering multiple audio clips to play in sequence 1 Answer


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