So I created a code for my gun with reload, rate of fire, range, and fire variables. However, when I reload, the rate of fire goes really high for some reason. Example: http://www.youtube.com/watch?v=PuiOOURL250.
I'm not sure why Fire() is a coroutine - you seem to be using CanFire as a lock around firing, but you're firing many coroutines potentially that will wait and then fire. There's also no check after your WaitForSeconds() to see whether there's any ammo left - you also deplete the ammo after the wait which I feel sure is going to lead to problems.
I'm suspicious as to why you need the check if AmmoLeft < 0 - i suspect your ammo keeps going negative and you've thrown this in to fix it, but this is something of a bandaid of the real problem.
Personally I would take the locking out, other than around reloading and replace the coroutine with a simple check for elapsed time, so you might have a variable so:
Which in Awake() you would set:
To capture when the player can next fire, then in your Update()
And then get rid of the coroutine in Fire() as you don't need a wait, your Update() is now limiting the rate of fire, but when you enter the Fire() routine you need to set:
In Update() at the top you can probably then:
Hopefully that helps.
Apologies if the code isn't quite right as I'm a C# bod really, but it should be obvious what I am suggesting I hope!
answered Jul 17 '12 at 05:29 AM