Ok so I’m fairly new to the Unity community and I’m trying to begin some sort of FPS, but something is just not working. After my reload animation, if I shoot (full auto) any time within like 2 seconds of when the reload animation ended, I can shoot millions of bullets out rapidly as if the WaitForSeconds as decided to not work. If I wait more than 4 seconds after reload, it works just fine with an even automatic fire. Help?!
Heres my script:
var Bullet : Rigidbody;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2.2;
var Ammo : float = 30;
var Spawn : Transform;
var IsFullAuto = true;
@script HideInInspector
static var ReloadTTime : float;
static var IsReloading = false;
static var AmmoLeft : float;
private var CanFire = true;
function Start () {
AmmoLeft = Ammo;
ReloadTTime = ReloadTime;
}
function Update () {
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0){
if(CanFire == true && IsReloading == false){
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
BroadcastMessage("ReloadAnim");
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire(){
var FireRate = 0.2;
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(Spawn.forward * BulletSpeed);
AmmoLeft -= 1;
audio.Play();
}
function Reload(){
CanFire = false;
IsReloading = true;
yield WaitForSeconds(ReloadTime);
AmmoLeft = Ammo;
IsReloading = false;
CanFire = true;
}