- Home /
Need help with AudioSources
I have three audio sources in my scene, each playing a bunch of audio clips playing on several occasions during gameplay using AudioSource.playOneshot(clipname, float) , also I have different AudioSource which plays background music on a continuous loop.
The problem is that some of the audio clips that are being played using the first three audio sources have a very low sound and can't be heard over the music, I need to have a script which detects if they're emitting sound and lower the volume of the music audio source then have it go back to normal when they are not.
would appreciate any help, thanks...
You would need a "Audio$$anonymous$$anager" class referencing a list of secondary audiosource and a main audiosource (for the background music).
then in an update or a coroutine check if any of the the secondary audiosource is playing (using AudioSource.isPlaying).
something like :
using UnityEngine;
using System.Collections;
public class Audio$$anonymous$$anager : $$anonymous$$onoBehaviour {
public AudioSource[] secondarySource;
public AudioSource mainSource;
private bool secondayIsPlaying;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
foreach (var source in secondarySource) {
if(source.isPlaying){
secondayIsPlaying = true;
break;
}else{
secondayIsPlaying = false;
}
}
if (secondayIsPlaying) {
mainSource.volume = 0.5f;
} else {
mainSource.volume = 1f;
}
}
}
This is just to get you started, It has not been tested. I also suggest a fade in/out animation ins$$anonymous$$d of a brutal change of the volume :)
Answer by Lord_Ford · Aug 19, 2015 at 05:56 AM
You can use the "Audio Mixer" in the editor. Watch these tutorials on the unity site
Cheers and have fun creating! :p