Post by tekustudios on Jul 30, 2015 7:53:54 GMT
Hi there!
I'm running into 2 annoying issues with audio in Unity and I'm pretty sure it must be a setup mistake on my side, so here I am trying to shed some light on this mystery (working in Unity v5.1.2f1 btw)!
First, there's the scene setup. We have all our groups placed as a child under a MasterAudio gameObject which also has a MasterAudio component that handles all those groups in the Group Mixer area. Everytime I load a checkpoint, or go to a door that loads a new scene, the game fades out to black, loads, and then fades in to the fresh scene. We want all the audio volumes to fade alongside the camera, and for that I do this:
So, as you can see, it is a pretty straight-forward code. In case you are not familiar with iTween, the ValueTo method simply lerps a float variable from the initial value to the target value in a specified time (and can call another funcion once it's completed). Well, this code is working, but I'm running into some annoying issue. Whenever I load a new scene, the audio starts playing for a brief fraction of a second, then goes silent and then starts fading in. It's like the audio is already playing at full volume, which is absurd since I have the scene setup for MasterAudio in the Editor like this:
The master volume is set to 0 and it is muted too, so when a scene loads I shouldn't hear anything, am I correct? Any ideas on what I might be doing wrong with this?
Then, we have another issue. There are some audios that sometimes can be heard and sometimes not. We are sure that those groups' configuration is correct, and our character (the listener) is in range and stuff, and I play them by using MasterAudio.PlaySound3DAtTransform("Whatever", transform) so it is played right in the spot (try to place them in the scene appropriately too, not miles away). There is, for example, a trap that kills you. I have a group placed at the same Transform position as the trap, and I play it from that transform as well. However, sometimes I can hear it and sometimes not. I've debugged my code and it is always called, and the group does not have random pitch or random volume values that could produce that erratic behaviour. Also, that kind of audio effects in our game always have the same setup: Volume 100%, no random pitch, no random volumes or delays or anything. What could be causing this? We've tried making the sound spheres gigantic (Min Distance - 50, Max Distance - 60) in case that was the problem, but the issue persists.
Also, sometimes the Volume option in the AudioSources suddenly jumps to 0. There is one sound that, everytime I hit Play, the Volume of the AudioSource jumps to 0 (but the MasterAudioGroup volume is at 1)! What am I doing wrong here?
Thanks for your time.
I'm running into 2 annoying issues with audio in Unity and I'm pretty sure it must be a setup mistake on my side, so here I am trying to shed some light on this mystery (working in Unity v5.1.2f1 btw)!
First, there's the scene setup. We have all our groups placed as a child under a MasterAudio gameObject which also has a MasterAudio component that handles all those groups in the Group Mixer area. Everytime I load a checkpoint, or go to a door that loads a new scene, the game fades out to black, loads, and then fades in to the fresh scene. We want all the audio volumes to fade alongside the camera, and for that I do this:
//Fade in everytime a scene gets loaded
void Awake()
{
//Force the audio to be silent before fading
MasterAudio.MixerMuted = true;
MasterAudio.MasterVolumeLevel = 0.0f;
Hashtable ht = new Hashtable();
ht.Add("from", 0.0f);
ht.Add("to", 1.0f);
ht.Add("time", 1.5f);
ht.Add("onupdate", "MasterVolumeLevel");
ht.Add("onupdateparams", MasterAudio.MasterVolumeLevel);
//Volume tweening
iTween.ValueTo(gameObject, ht);
MasterAudio.MixerMuted = false;
}
//Call this when I call my FadeOutAndLoad function
public void FadeOutOnLoad()
{
Hashtable ht = new Hashtable();
ht.Add("from", MasterAudio.MasterVolumeLevel);
ht.Add("to", 0.0f);
ht.Add("time", 1.5f);
ht.Add("onupdate", "MasterVolumeLevel");
ht.Add("onupdateparams", MasterAudio.MasterVolumeLevel);
ht.Add("oncomplete", "MuteScene");
//Volume tweening
iTween.ValueTo(gameObject, ht);
}
So, as you can see, it is a pretty straight-forward code. In case you are not familiar with iTween, the ValueTo method simply lerps a float variable from the initial value to the target value in a specified time (and can call another funcion once it's completed). Well, this code is working, but I'm running into some annoying issue. Whenever I load a new scene, the audio starts playing for a brief fraction of a second, then goes silent and then starts fading in. It's like the audio is already playing at full volume, which is absurd since I have the scene setup for MasterAudio in the Editor like this:
The master volume is set to 0 and it is muted too, so when a scene loads I shouldn't hear anything, am I correct? Any ideas on what I might be doing wrong with this?
Then, we have another issue. There are some audios that sometimes can be heard and sometimes not. We are sure that those groups' configuration is correct, and our character (the listener) is in range and stuff, and I play them by using MasterAudio.PlaySound3DAtTransform("Whatever", transform) so it is played right in the spot (try to place them in the scene appropriately too, not miles away). There is, for example, a trap that kills you. I have a group placed at the same Transform position as the trap, and I play it from that transform as well. However, sometimes I can hear it and sometimes not. I've debugged my code and it is always called, and the group does not have random pitch or random volume values that could produce that erratic behaviour. Also, that kind of audio effects in our game always have the same setup: Volume 100%, no random pitch, no random volumes or delays or anything. What could be causing this? We've tried making the sound spheres gigantic (Min Distance - 50, Max Distance - 60) in case that was the problem, but the issue persists.
Also, sometimes the Volume option in the AudioSources suddenly jumps to 0. There is one sound that, everytime I hit Play, the Volume of the AudioSource jumps to 0 (but the MasterAudioGroup volume is at 1)! What am I doing wrong here?
Thanks for your time.