- Home /
Gun with limited ammo?
Hello! I am testing a script of a weapon with "limited ammunition", it works that we place a quantity of bullets, when firing they are spent, and then we assign the amount of the recharge but that recharge is UNLIMITED, as I do so that it has a limit or spend? Thank you!
var speed = 10;//speed of bullet
var ammo:Rigidbody;//ammo
var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
var totalBullets = 30; //TOTAL amount the gun will have
var readynow : boolean = true;
var reloadammo : int = 30;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot
currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
}
}
}
function LateUpdate ()
{
if(readynow)
{
Update();
if(Input.GetKeyDown(KeyCode.R)){
currentBullets = (reloadammo);
readynow = true;
}
}
}
Answer by jman12EX · Dec 04, 2018 at 01:14 AM
try this has var speed = 10;//speed of bullet var ammo:Rigidbody;//ammo var currentBullets = 0;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.) var totalBullets = 30; //TOTAL amount the gun will have var readynow : boolean = true; var reloadammo : int = 30;
function Update ()
{
if(totalBullets >0) /////////// I addad
{
if(Input.GetButtonDown("Fire1"))
{
if (currentBullets > 0) //only shoot if you have ammo.
{
readynow = false;//can't shoot
var ammoClone : Rigidbody = Instantiate(ammo, transform.position, transform.rotation);
ammoClone.velocity = transform.forward * speed;
yield WaitForSeconds(.1);// pause between shots
readynow = true;//can shoot
totalBullets--; /////////// I addad
currentBullets--; //subtract one
}
else //if you have less than 1 bullet
{
//perform reload animation
}
}
} /////////// I addad
}
function LateUpdate ()
{
if(readynow)
{
Update();
if(Input.GetKeyDown(KeyCode.R)){
currentBullets = (reloadammo);
readynow = true;
}
}
}
if it worked could you accept my answer or tell me why it did not thanks
Your answer
Follow this Question
Related Questions
How can I shoot bullets that you can aim with the crosshair? 1 Answer
Gun script not working 3 Answers
not losing ammo when shooting 1 Answer
DmC: Devil May Cry Dual Shot? 0 Answers
Projectile Firing/Gun script 1 Answer