- Home /
Gun Ammo Help
Ok my problem with my script is thats it has a total of 30 bullets, if i shoot 16 bullets and press r i will still have 14 left. reloading only works if i have no bullets left. Can you tell me how to make it so every time i press r i have 30 bullets to shoot. Heres script...
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 30;
var shootSound : AudioClip;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
shots++;
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
shots = 0;
}
}
Answer by Ludiares.du · Jun 26, 2011 at 12:08 PM
Just change the ">" for "<" . Changing that you can reload every time you want.
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 30;
var shootSound : AudioClip;
function Update()
{
if(Input.GetMouseButtonUp(0) && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
shots++;
}
else if (shots <= maxShots && Input.GetKeyDown(KeyCode.R))
{
shots = 0;
}
}
Answer by psychentist · Jun 26, 2011 at 08:36 AM
Take out the "else" on the second if statement and it will work.
Answer by Joseph123122 · Jun 26, 2011 at 11:53 AM
What you put was var shots : int = 0; right well if you change the 0 to 14 then it will reload at 14 bullets. Here is an example var shots : int = 14; Hope it was useful.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122029im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
gun ammo script 2 Answers
Gun with limited ammo? 1 Answer
How do I make a gun project a particle? 1 Answer
how do i attach a gun to my character 2 Answers
Limited Fire Rate? 1 Answer