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 KnC_Studios · Aug 26, 2014 at 07:41 PM · audiosoundloop

Gunshot Sound Playing Too Many Times

I have a gun working right, and this is probably something simple I'm missing, but here goes. I have tried multiple version and the only one that plays any sound at all plays way too many. Its quite awful. All i want is for it too play the sound and continue to loop it until the gun is not firing anymore. For me it just plays a million sounds at a time. I am using the machine gun sound from the Angry Bots demo. Any ideas?

 var firing : boolean = false;
 
 var accuracy : float;
 
 
 private var bulletCaseGenerator : Transform;
 
 private var bulletTraceGenerator : Transform;
 
 private var muzzleFlashGenerator : Transform;
 
 private var bulletCaseGeneratorScript : bulletCaseGenerator;
 
 private var bulletTraceGeneratorScript : bulletTraceGenerator;
 
 private var muzzleFlashGeneratorScript : muzzleFlashGenerator;
 
 function Start(){
     
     bulletCaseGenerator = transform.Find("bulletCaseGenerator");
     
     bulletTraceGenerator = transform.Find("bulletTraceGenerator");
     
     muzzleFlashGenerator = transform.Find("muzzleFlashGenerator");
     
     bulletCaseGeneratorScript = bulletCaseGenerator.GetComponent("bulletCaseGenerator");
     
     bulletTraceGeneratorScript = bulletTraceGenerator.GetComponent("bulletTraceGenerator");
     
     muzzleFlashGeneratorScript = muzzleFlashGenerator.GetComponent("muzzleFlashGenerator");
     
     firing = false;
     //accuracy = 0.9;
 }
 
 function Update () {
     
     bulletCaseGeneratorScript.on = firing;
     
     bulletTraceGeneratorScript.on = firing;
     
     muzzleFlashGeneratorScript.on = firing;
     
     bulletTraceGeneratorScript.accuracy = accuracy;
     
     firing = false;
     
 
 
     
 }
 
 function Fire(){
     firing = true;
     
     animation.Play("gunFIRE");
     
     audio.Play();
     
 
 }
 
 function SetAccuracy(accuracyValue : float){
     
     accuracy = accuracyValue;
 }
 

Comment
Add comment · Show 6
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 tanoshimi · Aug 26, 2014 at 07:44 PM 1
Share

What actually calls Fire() ?

avatar image KnC_Studios · Aug 26, 2014 at 07:53 PM 0
Share

The weapon controller at the top of the hierarchy. The given script is attached to the gun of the model.

avatar image ReflectedMirror · Aug 26, 2014 at 07:54 PM 0
Share

^What he said

avatar image NoseKills · Aug 26, 2014 at 08:30 PM 0
Share

^ the Fire() method is the one playing the sound so it's hard to know why it's being played too much without seeing how it's being called at all :)

avatar image KnC_Studios · Aug 26, 2014 at 08:41 PM 0
Share

Alright. I transferred the audio.Play(); to the weapon controller. But it still plays too many times. i just want it to play once then loop. So here is the weapon controller code that signals "Fire":

 function Update () {
     var health : float = 100;
     if(healthScript != null){
         health = healthScript.GetHealth();
     }
     //Input.
 //    var isGrounded : boolean = soldier$$anonymous$$ovementScript.isGrounded;
     if (Input.Get$$anonymous$$ouseButton(0) /*&& !isSprinting && isGrounded && health > 0*/){
         firing  = true;
         gunSelectorScript.Broadcast$$anonymous$$essage("Fire",Send$$anonymous$$essageOptions.DontRequireReceiver);
         audio.Play();
     }
     else{
         firing = false;
         audio.Stop();
     }
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
     {
        Screen.showCursor = true;
     }

Show more comments

1 Reply

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

Answer by JustFun · Aug 26, 2014 at 09:07 PM

Change audio.Play() with this:

 if(!audio.isPlaying) audio.Play();
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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger audio loop on beat with PlayScheduled 1 Answer

Unity keeps altering my audio asset on import...? 3 Answers

How do I fix audio loop delay 2 Answers

Sound pause on audio loop? 2 Answers

Why Doesn't My Music Loop? 5 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