I have a 3-room area within my game, and none of them are simple shapes. What I’m trying to do is have separate background noise for each room, and fade them out when you leave one area and fade in when you enter another - it almost works. I have created multiple box triggers per room to create the different zones, and attaching all the game objects together under a parent running the script and the audio. The script is basically onTriggerEnter and Exit commands, but the triggers are going off whenever I cross boundaries from different triggers within the same zone. I thought that when you added colliders as children, they formed one complete trigger area - perhaps I am mistaken, or perhaps I am doing it wrong! I would appreciate any help.
public class MusicManager : MonoBehaviour {
public float musicFadeSpeed = 4.0f;
public float musicVolume = 0.4f;
public bool fadeUp;
public bool fadeDown;
void awake ()
{
fadeUp = false;
fadeDown = false;
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Player")
{
Debug.Log ("Set fade in to true");
fadeDown = false;
fadeUp = true;
}
}
void OnTriggerExit(Collider col)
{
if(col.gameObject.tag == "Player")
{
Debug.Log ("Set fade out to true");
fadeUp = false;
fadeDown = true;
}
}
void Update()
{
Debug.Log ("Test up?");
if(fadeUp == true)
{
Debug.Log ("Ready to fade!");
FadeIn ();
}
Debug.Log ("Test down?");
if(fadeDown == true)
{
Debug.Log ("Ready to fade!");
FadeOut ();
}
}
void FadeIn ()
{
fadeDown = false;
Debug.Log ("Should be fading in!");
audio.volume = Mathf.Lerp(audio.volume, 0.5f, musicFadeSpeed * Time.deltaTime);
}
void FadeOut ()
{
fadeUp = false;
Debug.Log ("Should be fading out!");
audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
}
}