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
0
Question by mcfetrmatt · May 26, 2012 at 10:37 PM · audiotriggerdelay

Delay audio collisions

Hi there,

As means of explaining my scene, I have two cubes (cube1 and cube2), a fountain particle system and audio for it. Cube1 is a trigger to allow cube2 to work, so cube2 will only trigger the fountain AFTER cube1 has been collided with, even if you walk into cube2 prior.

This works very well but the audio plays whenever you collide with cube2. Am I able to do something similar to the particles? Can I script the audio to only start once you collide with cube2 only after you have collided with cube1??

Hope this makes sense, my code is:

 var fountain: GameObject;
 var splash: GameObject;
 var enable: boolean = false; // boolean flag: enable trigger 1 when true
 
 function Awake(){
   fountain.particleEmitter.emit = false;
   splash.particleEmitter.emit = false;
 }
 
 // function called by trigger2 via SendMessage:
 
 function EnableTrigger(onOff: boolean){
   enable = onOff;
 }
 
 function OnTriggerEnter(trigger: Collider){
   if (enable){
     fountain.particleEmitter.emit = true;
     splash.particleEmitter.emit = true;
   }
 }
 
 function OnTriggerExit(trigger:Collider){
   if (enable){
     yield WaitForSeconds(2);
     fountain.particleEmitter.emit = false;
     yield WaitForSeconds(2);
     splash.particleEmitter.emit = false;
   }
 }

Code for Trigger2:

 var trigger1: GameObject; // drag trigger1 object here
 var trigger2: GameObject;
 var trigger3: GameObject;
 var trigger4: GameObject;
 var trigger5: GameObject;
 var trigger6: GameObject;
 var trigger7: GameObject;
 var trigger8: GameObject;
 var trigger9: GameObject;
 
 function OnTriggerEnter(other: Collider){
   // call EnableTrigger(true) in trigger 1
   trigger1.SendMessage("EnableTrigger", true); 
   trigger2.SendMessage("EnableTrigger", true);
   trigger3.SendMessage("EnableTrigger", true);
   trigger4.SendMessage("EnableTrigger", true);
   trigger5.SendMessage("EnableTrigger", true);
   trigger6.SendMessage("EnableTrigger", true);
   trigger7.SendMessage("EnableTrigger", true);
   trigger8.SendMessage("EnableTrigger", true);
   trigger9.SendMessage("EnableTrigger", true);
 }
Comment
Add comment · Show 1
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 aldonaletto · May 26, 2012 at 11:37 PM 1
Share

From the same post, "Optimization is not about avoiding expensive code. It's about avoiding expensive code where it matters". This code will be called infrequently, thus the perfomance gain using GetComponent is negligible.

1 Reply

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

Answer by aldonaletto · May 26, 2012 at 11:27 PM

If the audio is a loop (as fountain sounds usually are), you can Play the audio when start emitting particles, and Stop it when finishing emitting:

... function OnTriggerEnter(trigger: Collider){ if (enable){ fountain.particleEmitter.emit = true; fountain.audio.Play(); splash.particleEmitter.emit = true; splash.audio.Play(); } }

function OnTriggerExit(trigger:Collider){ if (enable){ yield WaitForSeconds(2); fountain.particleEmitter.emit = false; fountain.audio.Stop(); yield WaitForSeconds(2); splash.particleEmitter.emit = false; splash.audio.Stop(); } } ...

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 mcfetrmatt · May 26, 2012 at 11:41 PM 0
Share

Once again Aldo you make it look so easy!! Thank you very much :) Done and Done

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

6 People are following this question.

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

Related Questions

trigger audio and text from array 0 Answers

iOS - Audio Lag? 1 Answer

odd audio delay 1 Answer

AudioClip Precise Timing and Delay 1 Answer

Footsteps sound on collision problem, please help!!! 0 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