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
2
Question by panxter · Feb 28, 2013 at 04:48 AM · audiotrigger

Play audio-sources in trigger zone when player enters

Good morning/evening everyone!


I've run into a problem that appeared to be more of a mouthful than I thought. Trouble is, I have an amount of rigidbodies with attached audiosources, that the player is able to move around from one zone to the other.

I need a fail-proof solution, that plays the audiosources currently placed inside a trigger to start playing when the player enters the trigger, and stop when the player exits.

Since the audiosources are to be moved from one trigger to another, I cant just put the audiosources into a locked script that just plays a preselected amount of audiosources, but needs to be able to dynamically play only the audiosources currently inside the trigger zone.

I'm using C#, and I'm not quite sure on how to do this.

My current idea is next-to nonexistant, since my first plan went down the drain and failed completely.

Anyone able to point me in a direction for a good solution to my problem? Or have some bits of scripts to give me an idea of how to set this up?

Thanks in advance :)

Comment
Add comment · Show 3
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 Chronos-L · Feb 28, 2013 at 10:53 AM 0
Share

Do you $$anonymous$$d to reorganize your question, simplify your sentences and break it into few paragraphs?

Appending everything into one big pile of sentences will make any enthusiastic helping hands step back and rethink whether if it is worth their effort reading the whole thing.

avatar image panxter · Feb 28, 2013 at 11:19 AM 0
Share

There, fixed up the post. $$anonymous$$y apologies, rather new to this board and its way of formatting text.

avatar image Chronos-L · Feb 28, 2013 at 11:25 AM 0
Share

You will get used to it.

2 Replies

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

Answer by Chronos-L · Feb 28, 2013 at 11:09 AM

Assumption: You can setup the trigger.

Script

Audio Player: Attach this one to your audio source

 public class AudioPlayer : Monobehaviour {
    
    private int triggerCount = 0;
 
    void OnTriggerEnter( Collider collider ) {
       AudioHolder ah = collider.gameObject.GetComponent<AudioHolder>();
 
       if( ah != null ) {
          audio.clip = ah.playClip;
          audio.Play();
          triggerCount++;
       }
    }
 
    void OnTriggerExit( Collider collider ) {
      AudioHolder ah = collider.gameObject.GetComponent<AudioHolder>();
 
       if( ah != null ) {
          triggerCount--;
          if( triggerCount <= 0 ) {
             audio.Stop();
          }
       }
    }
 }

AudioHolder: Attach this one to the triggers

 public class AudioHolder : MonoBehavior {
    public AudioClip playClip;
 }


Explanation

  1. Everytime your audio source (which is moving, either by itself, or affected by its parent) enters a trigger zone [`OnTriggerEnter()`]

  2. it will look for the AudioHolder component [`GetComponent()`]

  3. If the trigger zone have that component (therefore, it is your trigger zone with sound, example, environment sound) [ if( ah != null ) ]

  4. The AudioPlayer will sets its clip to be the trigger zone's clip and play it. [ audio.clip = ah.playClip; audio.Play(); ]

I am throwing-in a triggerCount for the case of overlapping trigger zones(You can figure this one out yourself, just experiment with different trigger zone setup and comment-out some of the codes)

P/s: Code is not checked for syntax errors or actually tested.

Comment
Add comment · 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
1

Answer by aldonaletto · Feb 28, 2013 at 11:24 AM

Add a script to the trigger that adds any object of interest to a local list, and play/stop their sounds when the player enters/leaves the trigger - and also remove from the list the objects that exit the trigger. You should use some specific tag to identify the rigidbody+audiosource objects - "SoundSource", for instance:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class RegisterAudioSources: MonoBehaviour {
 
   private List<AudioSource> soundList = new List<AudioSource>();
 
   void OnTriggerEnter(Collider other){
     if (other.tag == "SoundSource"){ // sound object entering the trigger?
       soundList.Add(other.audio);  // add it to the list
     }
     else
     if (other.tag == "Player"){ // player entering trigger?
       foreach(AudioSource sound in soundList){ // play all sounds in the list
         sound.Play();
       }
     }
   }
 
   void OnTriggerExit(Collider other){
     if (other.tag == "SoundSource"){ // sound object leaving the trigger?
       soundList.Remove(other.audio); // yes: remove it from the list
     }
     else
     if (other.tag == "Player"){ // player leaving the trigger?
       foreach(AudioSource sound in soundList){
         sound.Stop(); // yes: stop all sounds in the list
       }
     }
   }
 
 }

This should work, unless the way the sound objects are moved by the user into the triggers doesn't generate OnTrigger events.

Comment
Add comment · Show 1 · 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 DoverWolf101 · Jun 03, 2019 at 03:09 PM 1
Share

Is there a way to a trigger an effect where the player walks through/on an object then the audio clip plays? Also, I don't know a thing about coding.

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

14 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

Related Questions

Audio Trigger 3 Answers

Audio Trigger Problems 1 Answer

Audio Clip constantly play 1 Answer

OnTriggerEnter2D --> One Shot Audio Problem 1 Answer

Wanting audio to pause when entering a trigger zone 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