- Home /
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());
}
}
}
Answer by Priyanshu · Aug 24, 2015 at 05:23 PM
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.
Otherwise if you want fade effect. You'll need AudioMixer. Here a decent Tutorial.
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.
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.