- Home /
Adding and Substracting ammunition
So to make a gun have limited ammo, you have to make an 'if' statement and write 'if(ammo >0)' then on next the line execute the shooting with a 'ammo --' line at the end to substract it from the 'ammo' variable.
Am i right, correct me if im not.
And then to add the ammo back, as if I am colliding with an ammo drop. I would write if the player has collided then we add the ammo like 'ammo++'
Correct me please. and this is how it should look like:
Ammo.js
var ammo
if(Input.GetButtonDown("mouse0"))
{
//Shooting
ammo--;
}
function OnCollisionEnter(col : Collision)
if(col.gameObject.tag == "AmmoDrop")
{
ammo++;
}
The shooting part of this script i can do it myself
looks pretty neat, are you having a problem with this ?
Answer by ytanay · Jan 01, 2013 at 09:55 AM
While it is generally preferred to use ammo -= 1
or ammo += 1
, and you didn't to declare ammo as an int (`var ammo : int - not actually required, but a very good idea`), to close the last function, and you didn't place the firing "if" statement in a function, the idea is correct.
As a general tip, I highly recommend you indent your code, and below is the corrected code:
var ammo : int;
function Update () {
if(Input.GetButtonDown("mouse0"))
{
//Shooting
ammo -= 1;
}
}
function OnCollisionEnter(col : Collision)
if(col.gameObject.name == "AmmoDrop"){
{
ammo += 1;
}
}
Your answer
Follow this Question
Related Questions
Can you determine the speed of falling? 3 Answers
add material to object when a collision occurs 2 Answers
Collision object tag 0 Answers
How to call only ONE OnCollisionEnter, on one of two objects 1 Answer