I’m very new to Unity, and I don’t know much about scripting.
I want to have different music play in different areas of a level. At the moment, I have one audio source in one area, and two duplicate ones in another (Because it is larger). The problem with this is that, due to their rolloff factor, they grow quiet very quickly but never quite fade out, which means you can hear the two different pieces (faintly) overtop of each other some times. This also results in hot spots where the music is a lot louder than everywhere else. Preferably, I would want to attach these sounds to the main camera and have scripting trigger them on and off, but I do not know how to script for that and it would be ideal for them to fade between one another, instead of simply stopping one and starting another. The only other way I could think to do this is to have a trigger which insatiates/destroys a game object with an audio source. I don’t know how to do this either, as I haven’t been able to get a trigger to destroy an object other than itself. I would again, want the music to fade. Any ideas?
Ok, I havent slept in a bit but I think I might have a solution. On either the audio sources or the player try putting in a Vector3.Distance script. and if the player is a certain distance away from the audio source it turns off, once they get back in range, it turns on again.
once again, sorry, too sleepy for posting script, not sure if this will help.
You shouldnt need to destroy the audio, if you look around enough there is a script that can set the volume of it to 0. I have never had luck making it work though (audio hates me).
http://forum.unity3d.com/viewtopic.php?t=6104&highlight=distance
that post has an example of the distance script
Well, I tink you’ll have to just assign the sound file to the audio source attached to the camera based on the area you are in.
As far as fading goes, are you doing it as background music, or as environmental music. For example, do you want once sound file for being in the wilderness, and another for eerie cave music, or are you talking about being in one room of a club playing country music, and moving into another room that is playing hip hop? (I guess what I am asking is, are you working with stero or mono sounds?)
If you want one to fade out completely, and then another fade in (stereo), you just need one audio source on the camera, and then a script that will react to a change in areas such that it gradually reduces the volume to 0. Then load the new sound file, and gradually increase the volume.
If you want them to mix as one fades out and the other in (stereo), create a second audio source on the camera. Then you gradually decrease one’s volume while you increase the other’s.
If they are mono sounds (directional), let whichever needs to roll off as far as it will go, then turn it off.
They are background music, converted from stereo to mono. The problem with them being mono is that the volume is all over the place depending on where the player is standing. Attaching them to the camera instead as stereo sounds would be great, but I don’t know how to script for trigger the change. One way it could work is if the player goes through a trigger game object, which then fades out the current music (a different trigger object) and fades in new stereo music (preferably the music should stay at the same volume level all the time, without panning) and vice versa. But again, I don’t know how to script this. So far, I haven’t been able to implement the Vector3.Distance code in relation to audio, as I know the least about audio scripting…