Hi,
I am building a scene with many audio sources (ambient sounds). But in the scene there are walls which define audio zones and some audio sources pass from one audio zone to the other because of the spherical form that defines the audio source delimitation.
I would like to mute zones of the audio sources so that when you are walking on one zone, all the audio sources that don´t must be heard, turn to mute.
I create a surface with the form of one audio zone (for example audio zone A) and I positioned it on the floor level. It is possible to turn to mute a selection of audio sources when crossing into these zone?
Hi, here is a script that solve this problem and allows you to create different audio zones being free to shape its forms. Thanks to Daniel Murcia!
For use it, divide in zones the audio sources that you want to be un-muted together. Create a surface (with the form of the zones you need) for each audio zone and positioned them on the floor level. Add a mesh collider to this surfaces, select-on the is trigger option and unselect the mesh render option. Aggregate to each surface the audio sources you want to be played on when enter in the area defined by the surface. Each surface or “audio zone” is going to be the father object of the audio sources you aggregate to it.
When you assign all audio sources you have in your scene to an audio zone, you are able to mute groups of audio sources, so when you are walking on the audio zone A, all the audio sources that don´t are assigned to this audio zone, turn to mute.
#pragma strict
var audioSources;
function Start(){
audioSources = gameObject.GetComponentsInChildren(AudioSource);
}
function OnTriggerExit(){
Debug.Log(“stopping sounds”);
Debug.Log(this);
var self = this;
for (var source:AudioSource in audioSources) {
Debug.Log(source);
source.Stop();
}
}
function OnTriggerEnter(){
Debug.Log(“starting sounds”);
Debug.Log(this);
var self = this;
for (var source:AudioSource in audioSources) {
Debug.Log(source);
source.Play();
}
}