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 Hamesh81 · May 25, 2013 at 02:47 PM · audiosoundtimercountdown

How can I play a sound in sync with a countdown?

I am trying to make a sound play based on the time left before a grenade explodes. When the boolean "pinPulled" is set to true a float starts counting down from 3.5 seconds. I would like the sound to play once every second until the last second when I want the sound to loop, so it beeps at a faster rate. Because there are other sounds that also use this audio source (grenade explosion sound, grenade throw sound etc), I assign the grenade beep sound dynamically before playing it. Below is a part of the script, the sound gets assigned properly based on the time but for some reason it doesn't actually play. Any ideas how I should do this?

I've also tried using Invoke, yield waitforseconds and a while loop but they all give inconsistent timing.

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 Fattie · May 25, 2013 at 09:00 PM 0
Share

simply use Invoke to do this sort of thing. It's incredibly easy to "script" games with it

 Invoke( "aBeep", 1 );
 Invoke( "aBeep", 2 );
 Invoke( "aBeep", 3 );
 Invoke( "finalWarning", 3 );
 Invoke( "aBeep", 4 );
 Invoke( "openGates", 4 );
 Invoke( "aBeep", 5 );
 Invoke( "beginAI", 5 );
 Invoke( "startEngines", 5 );

you see? ultra-easy.

1 Reply

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

Answer by aldonaletto · May 25, 2013 at 05:01 PM

The problem is that you're calling Play repeatedly when fuseTimer is lower than 2.5s: Play restarts the sound each time it's called, thus if the sound has an initial pause or a slow attack nothing will be heard. You could call Play only once each time you cross a limit, like this:

 var pinPulled : boolean = false;
 var fuse : float = 3.5;
 var fuseTimer : float = 0;
 var grenadeBeep : AudioClip;
 private var nextTime : float = -1; // declare this variable outside any function

 //If a grenade's pin has been pulled, start the countdown
 if (pinPulled) {
    fuseTimer -= Time.deltaTime;
 }
 else { // pin in place: reset fuseTimer and nextTime
    fuseTimer = fuse;
    nextTime = fuseTimer - 1.0; // first tick one second after pin pulled
 }
 
 if (fuseTimer <= nextTime) { // it's time to tick:
    nextTime = fuseTimer - 1.0; // set next tick to 1 second ahead
    if (nextTime < 0){ // if entered fast range (last second), set loop
        audio.loop = true;
    }
    audio.clip = grenadeBeep;
    audio.Play(); // call Play only once
 }
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 Hamesh81 · May 25, 2013 at 05:33 PM 0
Share

Brilliant! But in the last second the loop isn't actually making the beep speed up. How could I in the last second make the beep happen every half a second ins$$anonymous$$d of every second?

avatar image aldonaletto · May 26, 2013 at 03:06 PM 0
Share

It should beep continuously, since loop is set during the last second. Anyway, you could change the code a little and implement a fast pace in the last second:

 var pinPulled : boolean = false;
 var fuse : float = 3.5;
 var fuseTimer : float = 0;
 var grenadeBeep : AudioClip;
 var slowInterval : float = 1.0; // slow speed
 var fastInterval : float = 0.2; // fast speed
 private var nextTime : float = -1; // declare this variable outside any function
  
 //If a grenade's pin has been pulled, start the countdown
 if (pinPulled) {
    fuseTimer -= Time.deltaTime;
 }
 else { // pin in place: reset fuseTimer and nextTime
    fuseTimer = fuse;
    nextTime = fuseTimer - slowInterval; // set time for 1st tick
 }
  
 if (fuseTimer <= nextTime) { // it's time to tick:
    if (fuseTimer < 1.0){ // if entered fast range (last second), use fast interval
        nextTime -= fastInterval;
    } else { // otherwise beep at slow interval
        nextTime -= slowInterval;
    }
    audio.clip = grenadeBeep;
    audio.Play(); // call Play only once
 }

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

15 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

Related Questions

Play Audio at the end of a Countdown 0 Answers

Timer inexplicably counting down slower after it reaches a certain number. 0 Answers

Timer Scripting No Idea On Script 0 Answers

NGUI Volume And Quality Controle With Sliders Not working 1 Answer

Play from a variety of sounds? 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