Need help with automatic weapon firing!
Hello! I have a problem. My automatic weapon fires way too fast and whenever it shoots the sound doesnt finish. It's like "ba-ba-ba-ba-ba-ba-ba-bang (bang is when I let go of left mosue button) I don't know how to fix this so any help would be appreciated. Here is my code: function Update () { if (Input.GetButton("Fire1")) { var gunsound : AudioSource = GetComponent. (); gunsound.Play(); GetComponent. ().Play("MachineGin_shoot"); GlobalAmmo.CurrentAmmo -= 1; } }
Answer by SMJMoloney · May 14, 2016 at 10:53 PM
There's two ways to go about this. If you simply want to let the audio play you can say this
function PlayGunSound()
{
if (gunsound.isPlaying) return;
gunsound.Play();
}
This will ensure that the full sound plays.
If you want to slow down the gun and be sure the full sound plays, you'll need a timer. Declare a two floats at the top like this
public var speed;
var timer;
In the Update function, say this
timer += Time.deltaTime;
Finally, when you fire the gun say this
if (Input.GetButton("Fire1") && timer >= speed)
{
PlayGunSound();
timer = 0;
}
In the editor, you can say how fast the gun is by setting the speed value.
UPDATE:
You have to use a line like this
var gunsound : AudioSource = GetComponent(AudioSource);
You can declare that at the top and it should get your audio source.
In the end, it could look something like this
#pragma strict
public var speed;
var timer;
var gunsound : AudioSource;
var anim : Animation;
function Start()
{
gunsound = GetComponent(AudioSource);
anim = GetComponent(Animation);
}
function PlayGunSound()
{
if (gunsound.isPlaying) return;
gunsound.Play();
anim.Play("MachineGin_shoot"); //Should that be MachineGun?
GlobalAmmo.CurrentAmmo--;
timer = 0;
}
function Update()
{
timer += Time.deltaTime;
if(Input.GetButton("Fire1") && timer >= speed)
PlayGunSound();
}
Whenever I do what you say S$$anonymous$$J, I get "Object reference not set to an instance of an object" and The animation and sound wont work.
I've updated the original post. I don't know if that GlobalAmmo.CurrentAmmo statement works. I'm not sure of your setup but is GlobalAmmo another script attatched to the same gameObject?
No Global Ammo isn't attached to the same GameObject
Don't worry about being new. Gotta start somewhere.
Simply saying GlobalAmmo.CurrentAmmo won't work. I recommend putting an ammo variable in this script also like this
public var speed, maxAmmo;
var timer, currentAmmo;
function Start()
{
currentAmmo = maxAmmo
}
function PlayGunSound()
{
if (gunsound.isPlaying) return;
gunsound.Play();
anim.Play("$$anonymous$$achineGin_shoot"); //Should that be $$anonymous$$achineGun?
currentAmmo--;
timer = 0;
}
If you must use the other script, get it like the AudioSource and Animation
var globalAmmo : GlobalAmmo;
function Start()
{
globalAmmo = GameObject.Find("NameOfObjectScriptIsOn").GetComponent(GlobalAmmo);
}
function PlayGunSound()
{
if (gunsound.isPlaying) return;
gunsound.Play();
anim.Play("$$anonymous$$achineGin_shoot"); //Should that be $$anonymous$$achineGun?
globalAmmo.currentAmmo--;
timer = 0;
}
Note: for globalAmmo.currentAmmo--; to work, currentAmmo has to be public in the globalAmmo script. You're using Javascript so I'm not entirely sure it's necessary to say public. I use C# mostly so I'm not as familiar.
Okay the variable in my GlobalAmmo script is public. I have no compiler errors and my gun is working animation and sound, but when I go to the inspector on my game object and change my speed of the gun, the speed wont change. It waits for the sound to be completely over before I can shoot again. I want the sounds to overlap each other, like a real gun would.
I also get, "GetComponentFastPath can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene."
Code right now (Non-working) public var speed; var timer;
function Update () { timer += Time.deltaTime; if(Input.GetButton("Fire1") && timer >= speed) { var gunsound : AudioSource = GetComponent.(); gunsound.Play(); GetComponent.().Play("$$anonymous$$achineGin_shoot"); GlobalAmmo.CurrentAmmo -= 1; } }
Answer by Unity_scat · May 14, 2016 at 10:53 PM
Set up a delay system. Something like:
var lastShot : float = 0;
var delay : float = 0.2f;
function Update() {
lastShot -= Time.deltaTime;
if(Input.GetButton("Fire1") && delay <= lastShot) {
//firing code
lastShot = delay;
}
}