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 VictoryX · Jun 15, 2012 at 04:05 PM · audiohowwaittofinish

Wait Until Sound is Finished

So I have a Custom Weapon with Custom Animation attached to my player. When the player Left-Clicks it plays the animation and sound. However if the player repeatedly left clicks the sound restarts over and over again interrupting itself. How can I tell the engine in JavaScript, to wait a certain amount of time before playing the sound again? I'm using the sound attached to the weapon as a AudioSource Component. Here is the code I'm using to play the animation and sound.

function Update () {

 if(Input.GetButtonDown("Fire1"))
 {
        
        animation.Play("Punch");
     audio.Play();
     
 }

}

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 · Jun 15, 2012 at 09:26 PM 0
Share

you just use Invoke to call a "callback" that let's you know it is finished. sadly you just have to manually insert the known time of the sound

2 Replies

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

Answer by fafase · Jun 15, 2012 at 08:09 PM

 function Update () {
 
 if(Input.GetButtonDown("Fire1")&& !animation.isPlaying)
 {    
     animation.Play("Punch");
     audio.Play();
 }   
 }

It will check if the button is pressed AND if the animation is not playing. If it is still paying it returns false, no action.

Edit: You have the same members variable for audio.isPlaying. Note it is not a function so no ()

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 VictoryX · Jun 16, 2012 at 01:52 AM 0
Share

your suggestion worked perfectly now my Weapon is animating and playing sound as it should. Thank you very much good sir or madam!

avatar image
0

Answer by Jessica_Ann · Jun 15, 2012 at 07:46 PM

You probably want to use Wait for seconds. It stops and waits for a passed in amount of time.I linked the scripting help for you. Hope this helps.

WaitforSeconds

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 fafase · Jun 15, 2012 at 08:14 PM 0
Share

Well,....Problem would be that every time you would press the button it would not keep from acting but just wait for x seconds, meaning that if you press 10 times, you will have 10 actions after x seconds. Or you would have to add a boolean.

avatar image Jessica_Ann · Jun 15, 2012 at 08:17 PM 0
Share

Yeah you might be right. Good catch

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

how to make make a game wait for 10 seconds the change level 2 Answers

How to open a .unity3d file 1 Answer

how to lock screen? 1 Answer

audio.Play ignoring WaitForSeconds 2 Answers

CREATING A TERRAIN 3 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