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 TheRichardGamer · Aug 07, 2013 at 05:22 PM · soundgunweapon

Automatic gun sound freaking out

Hi, I have a serious problem, whenever I try to fire my gun in unity the sound plays as it should and that, but after 5 seconds or so the sound starts to freak out, and I have no idea why. Here's what happens when I hold down LMB: http://yourlisten.com/TheRichardGmaer/educational-only-weird-firing-sound-in-unity-3d

I have attached a audio source to my bullet prefab that is checked 'Play on awake' and I have also tried 'audio.PlayOneShot(G36C_Sound);' but that doesn't work either. If anyone could help me solve this problem I would be so thankful!

Thanks in advance!

Comment
Add comment · Show 2
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 TheRichardGamer · Aug 07, 2013 at 05:28 PM 0
Share

Ok, here's my code, and btw ignore the comments var Bullet : Rigidbody; var $$anonymous$$ag = 26; var AmmoInPocket = 26; var BarrelEnd : Transform; var G36C_Sound : AudioClip; var G36C : GameObject;

 var FireSpeed : float;
 var WaitTillNextFire : float;
 
 
     var fireRate : float = 0.5;
     private var nextFire : float = 0.3;
     function Update () {
         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             BulletInstance = Instantiate(Bullet, BarrelEnd.position, BarrelEnd.rotation);
         BulletInstance.AddForce(BarrelEnd.forward * 8000);
         audio.PlayOneShot(G36C_Sound);
         
         
         }
     }
 
 
 
 
 
 
 
 
  //if(Input.GetButton("Fire1")){
  
       //var BulletInstance : Rigidbody;
      
      //if(WaitTillNextFire <= 0){
      
      //}
        //G36C.animation.Play();
        //audio.PlayOneShot(G36C_Sound);
      
         //BulletInstance = Instantiate(Bullet, BarrelEnd.position, BarrelEnd.rotation);
         //BulletInstance.AddForce(BarrelEnd.forward * 8000);
         //WaitTillNextFire = 1;  
     
  //}
  //WaitTillNextFire -= Time.deltaTime * FireSpeed;
 
avatar image robertbu · Aug 07, 2013 at 05:48 PM 0
Share

I don't see a problem. You might have an issue if fireRate is adjusted anywhere. Check the AudioSource and make sure loop is turned off while this behavior is going on. I assume it is just the sound and the gun is not also firing?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ivan2532 · Aug 07, 2013 at 05:35 PM

Hello, TheRicharGamer, this is the code that probably works:

 var CoolDown : float; //How much seconds is delay
 private var Cooler : float;
 
 function Update()
 {
     if(Cooler == 0)
     {
         Cooler = 0;
     }
 
     if(Cooler > 0)
     {
         Cooler -= 1 * Time.deltaTime;
     }
 
     if(Input.GetMouseButton(0) && Cooler == 0)
     {
         Fire();
     }
 }
 
 function Fire()
 {
     BulletInstance = Instantiate(Bullet, BarrelEnd.position,          BarrelEnd.rotation);
     BulletInstance.AddForce(BarrelEnd.forward * 8000);
     audio.PlayOneShot(G36C_Sound);
 }

Ivan Bozovic

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 TheRichardGamer · Aug 07, 2013 at 05:41 PM 0
Share

Nope, the sound still fucks up.

avatar image ivan2532 · Aug 07, 2013 at 05:52 PM 0
Share

Sorry, i don't know...

avatar image
0

Answer by ExplodingCookie · Aug 07, 2013 at 08:10 PM

I have encountered this issue before. If there are too many oneshot audio sources in the scene, Unity tries and fails to play them all. There are multiple solutions.

1.

Make sure that the audio clips you are using do not have large amounts of silence after the sound effect.

2.

Have the shoot sound in an audio source where the gun script is, then call the audio.Play()

3.

Reduce the fire rate so the oneshots will have time to die

Option 1 means that if you have the sound effect followed by a large bit of silence in the same file, it will take a log time for the oneshots to die.

Option 2 will cut the shot sound short, so it is not recommended for sound effects that play frequently.

Option 3 is really only and option for slow firing weapons like rilfes or shotguns.

Hope this helps you a bit.

~ExplodingCookie

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

Answer by wevenstall · Aug 17, 2019 at 03:01 AM

 [SerializeField] private AudioClip turretSound;
 [SerializeField] private float coolDown;
 private float cooler; 
 
 private void PlayTurretSound()
     {
         if (cooler <= 0)
         {
             GetComponent<AudioSource>().PlayOneShot(turretSound);
             cooler = coolDown; 
         }
         else
         {
             cooler -= Time.deltaTime; 
         }  
     }


You can tweak your cooldown time in the inspector against your audio clip. Just make sure you have an AudioSource component attached to the scripts parent "gameObject". Not sure if this is the best way but it works.

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

Answer by Meishin · Aug 17, 2019 at 08:32 AM

Hi @TheRichardGame,

Did you try the following (last included in previous comment) :

Latency is set to "best"

Sounds are "decompress on load" and "preload audio data"

Your sound clip is .wav file and do not have any baked delay in it - starts at 0 and ends right after the sound

Also note that : audio.PlayOneShot(G36C_Sound) with an already defined Audio Source is better than creating a new audio source on each bullet and play the sound on awake.

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

18 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

Related Questions

Question about Gun Sounds 1 Answer

Automatic repeated sound 1 Answer

How do I add a gunshot sound to this? 2 Answers

Help with Lerp 0 Answers

Firing Sound 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