I have my shooting script, and to limit ammo:
#pragma strict
var projectile : GameObject;
var fireRate : float = 0.1;
private var nextFire : float = 0.0;
var hasMuzzleFlash : boolean = true;
var muzzleFlash : GameObject;
var bulletsLeft : int = 5;
private var hasFired : boolean = false;
private var bulletsPerShot : float = 1;
function Update () {
if(hasFired) {
bulletsLeft -= bulletsPerShot;
}
if(Input.GetKey("space") && bulletsLeft <= 0) return;
if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0) {
nextFire = Time.time + fireRate;
var clone = Instantiate (projectile, transform.position, transform.rotation);
hasFired = true;
}
if(Input.GetKey("space")) return;
if(Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
Instantiate (muzzleFlash, transform.position, transform.rotation);
}
}
To make sure that the shooting stopped if ammo ran out, I made it return in that situation:
if(Input.GetKey("space") && bulletsLeft <= 0) return;
Inside the firing script I made a boolean to make sure it actually fired:
if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0) {
nextFire = Time.time + fireRate;
var clone = Instantiate (projectile, transform.position, transform.rotation);
hasFired = true;
And I allowed it to calculate the bullets:
if(hasFired) {
bulletsLeft -= bulletsPerShot;
}
Well the only problem is, when I fire once, it never stops subtracting. It begins subtracting and subtracting and it never stops. SO i put it in a FixedUpdate() function (I wasn’t sure how that would help, but it’s worth a try), and that didn’t change. So I put it in it’s own function called function BulletCounter()but then it didn’t work at all.