Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Jigglebones · Jan 26, 2015 at 09:29 PM · javascriptaudioaudiosourceloopgunfire

How do I prevent audio from restarting if it's being asked to play again?

Basically I'm trying to add sound to my fully automatic M4. I have a sound file of a single shot from an M4 which I'm using. I made a script that tells the sound to play every few seconds as long as mouse button 1 is down. The problem is that instead of the sound playing out fully with the new one playing on top of it a few seconds later, the sound just restarts constantly without playing out fully. It works fine only if my weapon is semi auto with a RoF longer than the sound file, but my weapon has a high RoF. Here is the script:

 #pragma strict
 var gunSmoke : ParticleSystem;
 var isFiring = false;
 
 function Update () 
 {
     if (Input.GetMouseButtonDown(0))
     {
         isFiring = true;    
         InvokeRepeating("ShotSound",0,0.3); //This also acts as RoF
         gunSmoke.Play();
     }
     if (Input.GetMouseButtonUp(0))
     {
         isFiring = false;
         CancelInvoke();
         gunSmoke.Stop();
     }
 }
 
 function ShotSound ()
 {
     if (isFiring == true)
     {
         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 meat5000 ♦ · Jan 26, 2015 at 11:02 PM 0
Share

If you load audioclip and play you will see the behaviour you describe. Try playing it with OneShot ins$$anonymous$$d.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Simon-Larsen · Jan 26, 2015 at 10:12 PM

You could check if the audio is already playing

 if (!audio.isPlaying)
     audio.Play();
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 Simon-Larsen · Jan 26, 2015 at 10:13 PM 0
Share

You may also want to enable the "looping" flag when firing starts and then disable it again when firing stops - only calling Play() between starting and stopping once.

avatar image
0

Answer by WerewoofPrime · May 15 at 05:53 PM

https://gamedevbeginner.com/how-to-play-audio-in-unity-with-examples/#clip_at_point

i found this! watch the part of the video about PlayOneShot, it overlaps sounds if one is already playing instead of restarting them :D

Comment
Add comment · 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

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

Sound wont play when triggered (javascript) 1 Answer

How do I only start the audio when player is at max distance 1 Answer

Can I play multiple AudioSources from one gameobject? 8 Answers

Error BCE0023 when using script for main menu 1 Answer

Audio loops too early 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