Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by permanentrecords · Oct 18, 2013 at 08:20 PM · triggersmultipleshapes

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);    
     }
     
     
 }
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zombience · Oct 18, 2013 at 09:06 PM 0
Share

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?

avatar image permanentrecords · Oct 18, 2013 at 09:35 PM 0
Share

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.

avatar image zombience · Oct 18, 2013 at 09:50 PM 0
Share

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?

avatar image permanentrecords · Oct 18, 2013 at 10:16 PM 0
Share

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.alt text

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image permanentrecords · Oct 18, 2013 at 11:55 PM 1
Share

Thank you very much. It should only take me a $$anonymous$$ute to make the new trigger.

avatar image animalphase · Oct 19, 2013 at 12:05 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges