- Home /
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!
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;
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?
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
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
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.
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.
Your answer
Follow this Question
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