Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Patchbristol · Aug 10, 2016 at 03:12 PM · audiotriggeraudiosourceoculusboxcollider

Correct code for triggering audio source by walking through box colliders (I'm using Oculus Native Spatialiser)

Hello,

I am aware variations on this question has been asked and answered numerous times, and I have spent the past 24 hours trying to make the answers solve my problem but to no avail, so I'm asking my own question with the hope you guys can help me solve it

I am doing resit coursework for 2nd year of my degree and my project is due in a few days so would be really grateful for some help on this.

I am populating a game level with various one shot SFX attached to numerous box colliders with audio source components on them. I want my player to trigger the audio clip attached to the audio source whenever I walk through the box collider. I am using the Oculus Native Spatialiser so I fear the code may have to include reference to this fact but I'm not sure as all I want the code to do is trigger the audio clip attached to the audio source to play.

I'm using Unity 5.2.1f1

Many thanks for your help,

Patrick

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 Patchbristol · Aug 10, 2016 at 02:38 PM 0
Share

After finding some answers, I have added this script to a box collider (with audio source attached as well as the Oculus ONSP audio source script) but it doesn't work

 using UnityEngine;
 using System.Collections;
 
 
 public class playclip1 : $$anonymous$$onoBehaviour {
     public AudioSource source;
     
     public void OntriggerEnter(Collider player) {
         source.Play ();
         
     }
 }    

avatar image Patchbristol Patchbristol · Aug 10, 2016 at 03:00 PM 0
Share

I have also tried this js script, again to no avail`var soundFile:AudioClip;

 function OnTriggerEnter(trigger:Collider) {
     if(trigger.collider.tag=="Player") {
        audio.clip = soundFile;
        audio.Play();

     }

}`

avatar image Patchbristol · Aug 11, 2016 at 06:45 PM 0
Share

Hi @crazy$$anonymous$$night or @$$anonymous$$ikeNewall, would you be able to shed some light on this? Thanks, Patch

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Patchbristol · Aug 13, 2016 at 07:21 PM

Someone helped me and made a working code.

For anyone interested I hope this helps.

 using UnityEngine;
 using System.Collections;
 
 public class PlaySoundOnCollision : MonoBehaviour
 {
 
 
 
     [SerializeField]
     private AudioClip[] _audioClip;
     private AudioSource _audioSource;
     void Start ()
     {
         _audioSource = GetComponent<AudioSource>();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
            
             _audioSource.Play();
         }
     }
 }
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 Patchbristol · Aug 13, 2016 at 06:49 PM 0
Share

You just need to change the public class name on line 4 to what ever your script is called. So ins$$anonymous$$d of, '.....PlaySoundOnCollision.....' - replace that with your script name.

avatar image KOCGI · Aug 30, 2018 at 12:15 AM 0
Share

Working? All it does is nothing at all

avatar image
0

Answer by FabriBertani · Aug 12, 2016 at 09:44 AM

Try this (is c#):

 using UnityEngine;
 using System.Collections;
 
 public class playclip : MonoBehaviour {
     public AudioClip yourAudioClip;
 
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "Player") {
             GetComponent<AudioSource> ().clip = yourAudioClip;
             GetComponent<AudioSource> ().Play ();
         }
     }
 }

And you must drag your audioclip to script component in the inspector. Hope it help.

Comment
Add comment · Show 4 · 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 Patchbristol · Aug 12, 2016 at 10:13 AM 0
Share

Hi Fabri, many thanks for the code. It's giving me an error which you can see in the picture

I've added the cs script to my box collider, checked 'is trigger', and added an audio source to the box collider with the audio clip added.

In the script, I changed the public class from playclip to playclip3 as that is what the script is called, I've changed line 5 'yourAudioClip' to 'siren1', and I've changed line 9 yourAudioClip' to 'siren1' also.

Thanks,

Patch! alt text

screen-shot-2016-08-12-at-110740.png (428.5 kB)
avatar image Patchbristol · Aug 12, 2016 at 10:14 AM 0
Share

The 3rd error in the console is related to a door and is unrelated to my problem

avatar image Patchbristol · Aug 12, 2016 at 10:16 AM 0
Share

Are my revisions correct?

avatar image Patchbristol · Aug 12, 2016 at 11:19 AM 0
Share

I got it to work.. I made a new script and changed the name to oneshot, whilst also changing the public class in the script to match - perhaps it didn't like the number part of the old name?

I put my SFX into the Your Audio Clip box and it played correctly, I'm very happy but wonder if in doing so this negates all of the functions of the Audio Source component? When I adjust the pitch or volume in the Audio Source for example the sound no longer plays. I need to do things such as send the sound to a mixer, change pitch, adjust the spatial blend

Is there a way to just reference the AudioClip in the Audio Source?

$$anonymous$$any thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerExit stop audio 1 Answer

Play Audio Sound OnTriggerEnter 2 Answers

I've got strange sound distorsion when I trigger a sound 0 Answers

Why audio is getting distorted at a certain point in Oculus Quest? 0 Answers

Problem with playing sound on Trigger-Enter 2 Answers


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