- Home /
Shooting with Limited Ammo
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.
Answer by SilverTabby · Sep 24, 2011 at 12:51 AM
Your problem is that you are using "hasFired" in order to check if a bullet was fired this round. By itself it's OK, but the problem is that you never do this if a round was not fired:
hasFired = false;
I would recommend the following to fix your repeat fire problem:
At the start of Update,
hasFired = false;
Move "
if(hasFired)
" to the bottom of Update
Another thing I want to point out about this script:
from this if block (the final one in Update):
if(Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
Instantiate (muzzleFlash, transform.position, transform.rotation);
}
Move the Instantiate call to hasFired = true;
block (the third one in Update); then delete the second if(Input.GetButton("Fire1"))
block in update - it is unnecessary and redundant after moving the Instantiate call elsewhere
Remember to check an answer if it answered your question: it helps optimize the search system, declares a question as complete, and it gives me a nice little karma cookie :)
Answer by ocularcash · Sep 24, 2011 at 12:41 AM
i personally use
bulletsleft --;
if(bulletsleft < 0){
bulletsleft = 0;
}
if(bulletsleft == 0){
Reload();//if you have a reload function
}
}
Your answer
Follow this Question
Related Questions
How To Add A Limit To Amount Of Objects 1 Answer
Issues with bullet not subtracting properly 1 Answer
Fire fixed number of bullets 1 Answer
2D game how to make my player shoot 1 Answer