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 Ful · May 06, 2013 at 11:07 PM · raycastaudiomultiplerepeat

How do you prevent audio from being played multiple times when using raycast? (JS)

Hello Everyone,

I'm currently trying to prevent my audio from playing multiple times whenever the player interacts with one of the my gameobject's, however I'm having difficulties on doing so.

I'm using a raycast system that whenever the player presses the "E" or "left mouse", this allows him to interact with several objects in my scene. In particular interacting with one of a wardrobe door will cause it to open. Interacting with it once again will cause it to close instead, and vice versa... Therefore as you can see from the code below, I created some booleans to allow the door to open and close, but also added the yield WaitForSeconds (transform.parent.animation.clip.length); to prevent the player from interrupting their animations.

This code is indeed working perfectly however I'm having some trouble to prevent the audio from being played multiple times whenever the player repeatedly presses the usage key. I did look into audio.length, however such attempts were unsuccessful. Therefore I'm asking if anyone knows any other methods where i can achieve this or perhaps I should retry the audio.length command?

Thank you!

 #pragma strict
 
 private var cupisOpen : boolean = false;
 
 var WarAOpen : boolean = false;
 var WarBOpen : boolean = false;
 
 var CupOpenSound : AudioClip;
 var CupCloseSound : AudioClip;
 
 
 function WarOpen1 () {
 
     if (!WarAOpen){
     transform.parent.animation.Play("War1Open");
     audio.PlayOneShot(CupOpenSound);
     yield WaitForSeconds (transform.parent.animation.clip.length);
     WarAOpen = true;
     
     }else{
     
     transform.parent.animation.Play("War1Close");
     audio.PlayOneShot(CupCloseSound);
     yield WaitForSeconds (transform.parent.animation.clip.length);
     WarAOpen = false;
     
     }
 }
 
 function WarOpen2 () {
 
     if (!WarBOpen){
     transform.parent.animation.Play("War2Open");
     audio.PlayOneShot(CupOpenSound);
     yield WaitForSeconds (transform.parent.animation.clip.length);
     WarBOpen = true;
     
     }else{
     
     transform.parent.animation.Play("War2Close");
     audio.PlayOneShot(CupCloseSound);
     yield WaitForSeconds (transform.parent.animation.clip.length);
     WarBOpen = false;
     
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by llSalvationll · May 06, 2013 at 11:24 PM

Use the isPlaying function of the animation. Given what you've got, try something like this if(!transform.parent.animation.isPlaying("War1Open") && !transform.parent.animation.isPlaying("War1Close")) { // play your sound } http://docs.unity3d.com/Documentation/ScriptReference/Animation.IsPlaying.html

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 Ful · May 06, 2013 at 11:46 PM 0
Share

Thanks for the replay, however I'm receiving the following error: It is not possible to invoke a type expression of type boolean.

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

13 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

Related Questions

How to play several audio cllips one after another. 3 Answers

i have a error that can not play a disabled audio source 1 Answer

Audio script doesn't work 0 Answers

Audio after Audio Loop 1 Answer

Play back multi channel audio 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