- Home /
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;
}
The weapon controller at the top of the hierarchy. The given script is attached to the gun of the model.
^ 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 :)
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;
}
Answer by JustFun · Aug 26, 2014 at 09:07 PM
Change audio.Play() with this:
if(!audio.isPlaying) audio.Play();
Your answer
Follow this Question
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