x


Gun Ammo help in JS

This is my first post and I need help with getting a ammo count that will decrease every time a bullet is fired. I would aslo like to get a reload function that would sutract from a clipsLeft var and reset the ammo var. this is what I have so far.

`var projectile : Transform; var shootSpawn : Transform; var ammo : int = 6; var clipsLeft : int = 10;

function Update () { if (Input.GetButtonDown("Fire1")){ if (Ammo > 0){ clone = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation); Physics.IgnoreCollision(clone.collider, collider); //This is the subtraction function that I'm having trouble with. ammo -= 1; } else { print("Reload"); } } }`

If anyone could answer using Java Script it would be much appreciated!!!

more ▼

asked Nov 13 '10 at 01:47 PM

pdh1kid gravatar image

pdh1kid
54 8 9 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Does this do what you're looking for?

var projectile:Transform;
var shootSpawn:Transform;
var clipsLeft:int = 10;  // how many clips the player has
var clipSize:int = 6;    // how many bullets in 1 clip
private var ammo:int;    // how many bullets left in the current clip

function Start() {
    ammo = clipSize; // Start with this many bullets.
}

function Update() {
    if(Input.GetButtonDown("Fire1")) {
        if(ammo > 0) {
            var clone:Transform = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation);
            Physics.IgnoreCollision(clone.collider, collider);
            ammo--;  // Subtract 1 bullet
            if(ammo <= 0)
                Reload(); // Do you want to call reload here...
        }
        else {
            Reload(); // ...or here?
        }
    }
}

function Reload() {
    if(clipsLeft > 0) {
        clipsLeft--;
        ammo = clipSize;
        // Play sound/animation ?
    }
    else {
        // No more clips left...
    }
}
more ▼

answered Nov 13 '10 at 09:30 PM

Tom 10 gravatar image

Tom 10
287 2 3 7

For the most part yes! thank you!

Nov 14 '10 at 02:19 PM pdh1kid
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1197
x461

asked: Nov 13 '10 at 01:47 PM

Seen: 795 times

Last Updated: Nov 13 '10 at 01:47 PM