- Home /
Using multiple triggers as one trigger zone
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);
}
}
I'm a bit confused.. you have multiple colliders in one room all acting as on/off switches for the music of that room? or you have one empty game object that encapsulates all of the colliders in all of the rooms and manages the audio?
I have one game object with the script and audio source plus one box collider, and then as children of that more game objects with triggers to construct the shape of the room. I have also tried one empty game object with the script+audio attached and then only children carrying triggers with same results.
if this script you have posted is a parent gameObject with multiple child colliders, they will all report to this gameObject.
I'm not sure I understand your implementation, but why wouldn't you just have one giant collider per "zone"? What are you gaining from having multiple colliders per zone?
If I understand correctly, with your implementation I could walk into one room (one "zone"), and only have audio playing while I was, say, in the corner, and it would stop when I left that corner, but begin again when I entered a different corner. That's what I see implemented here. And that isn't what you want?
Perhaps my explanation was inadequate. The reason for the multiple triggers per zone is that the rooms aren't squares or rectangles, so I thought that by using multiple triggers I could mirror the shape of the room and therefore have the music playing when inside the trigger - using just one trigger to cover each room would not work due to the layout of the rooms. The audio zones would overlap. Basically what my question is, is there a way to combine multiple triggers into one? What are my alternatives? Here is a picture of the zones I was talking about.
Answer by animalphase · Oct 18, 2013 at 11:43 PM
I, too, have wondered about this in Unity. In the Source engine, for example, you can create multiple brushes and tie them all together as a single entity (and thereby a trigger collision will only fire when the player enters into the entirety of the volume, rather than separate collisions firing for each component brush).
A workaround:
Create a custom-shaped mesh in a modelling program to be the custom trigger shape that you need. Use this mesh as the physics collider mesh.
This involves a bit of extra work going back and forth between Unity and your modelling program, but it should work fine.
Another workaround:
Update your script to deactivate the parent gameObject after void OnCollisionEnter()
has been fired. This way, one trigger will send an output, then they will all be disabled.
To reuse the set of trigger volumes, you will have to re-enable the parent object after the player collides with another trigger set.
One potential bug: if the player collides with two trigger volumes in the same frame, it may still fire twice.
Thank you very much. It should only take me a $$anonymous$$ute to make the new trigger.
Cheers, good luck!
If you end up making a larger game that requires many, many changes in background sounds, you could look into implementing something that mimics Source's soundscapes: https://www.youtube.com/watch?v=ZgljW7nNhHg
Basically, they're point entities that just store a reference to a particular background track. When get within a pre-defined radius, the background ambient track automatically switches to the associated audio file.
You control it by smartly placing them throughout the level, so they player will automatically switch over when aligned in the proper spot.
Very similar to your trigger functionality, but I guess the key difference is they're point-based with a radius, vs. needing to define a specific collision mesh. This makes it a little easier to adjust in the editor on-the-fly.
Your answer

Follow this Question
Related Questions
If multiple triggers activated 1 Answer
Multiple OnTriggerEnter commands in one script? 2 Answers
Two trigger activation. 1 Answer
Switching Several Cameras(HorrorGame) 1 Answer
Detecting multiple gameobjects 1 Answer