- Home /
How to make an int not pass 0
I am making a zombie shooter and I'm having an issue where when i reload and per say i have a total of 5 bullets left, the clip size is 8 when i reload it will reload 8 bullets setting the total bullets remaining to -3 how can i make my script so it will never pass 0, and if there was say 2-3 bullets left in the stockpile, that when i reload it will only load those 2-3 bullets not a full clip.
Some coding to show where i am at..
if (reloading)
{
canShoot = false;
reloadTimer -= Time.deltaTime;
if(reloadTimer <= 0)
{
ammoClip -= origClip;
ammoClip += spareBullets;
bulletClip = origClip;
spareBullets = 0;
reloading = false;
canShoot = true;
reloadTimer = startRTimer;
}
}
public void reload()
{
if (ammoClip >= 0)
{
spareBullets = bulletClip;
aud.PlayOneShot(reloadSound);
canShoot = false;
reloading = true;
anim.Play("APCReload");
}
}
Let me explain.. spareBullets is say the clip holds 8 bullets, i shoot 5 and reload, it would have destroyed those 3 unused bullets, but spareBullets takes that number of bullets left in a clip while reloading and adds them to the stockpile...
ammoClip is total bullets.
bulletClip is currently loaded into the gun.
origClip is a memory to how many bullets will fit into a clip.
Answer by unit_nick · Oct 14, 2017 at 01:56 PM
if (reloading)
{
canShoot = false;
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0)
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
reloading = false;
canShoot = true;
reloadTimer = startRTimer;
}
}
public void reload()
{
if (magazineLevel < magazineCapacity)
{
magazineLevel = Mathf.Min(magazineCapacity, remainingBullets);
remainingBullets -= magazineLevel;
aud.PlayOneShot(reloadSound);
canShoot = false;
reloading = true;
anim.Play("APCReload");
}
}
i added logic in both places because you had 2 different bits of code, but you shouldn't use them both
as expected this does not work...
what this does is say i shoot 8 bullets, press r to reload, it $$anonymous$$us' 8 bullets from the cache on start of reloading, then when finished reloading it $$anonymous$$us' another 8 bullets, so now a clip is just being thrown away for no reasoning..
and even so if i shoot 1 bullet, and hit reload, it will delete the remaining 7 bullets that were in my clip, and replace them with 8 at start of reload, then later at finish of reload it will delete those 8 and take another 8 out of the stock pile and place them into the clip...
Video Clip to show whats happening... https://youtu.be/3ugc4xrULYY
I may have to explain again what is it that i am actually wanting to acheive....
I have 40 bullets in cache and 8 bullets in clip, i shoot 1 bullet making the clip now 7, when i reload i want the 7 bullets to be stowed away back into the cache making the cache now 47, now on finishing the reload i want to take the 8 bullets to fill the clip, finalizing the cache at 39 bullets remaining, with 8 bullets now in the clip.
Your coding is per say 'deleting' not stowing those 7 bullets, and instantly replacing them with 8 from the cache, then on finish of reloading deletes those 8 bullets it just reloaded and reloads another 8 bullets.
Correct... I showed 2 different implementations because you have doubled up on stuff. I even made the point of telling you I added the logic in both places and that you shouldn't use them both. But because of your attitude "as expected..." you can fix your own mistakes.
Answer by Bunny83 · Oct 22, 2017 at 03:07 PM
Your variable names are very confusing. You should use more descriptive names so it's more clear what the variable actually stores.
public int clipSize = 10;
public float reloadTime;
public int bullets;
public int clip;
private float reloadTimer;
void Reload()
{
if (clip < clipSize && bullets > 0 && !reloading)
{
reloading = true;
reloadTimer = reloadTime;
}
}
void Update()
{
if (reloading)
{
canShoot = false;
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0)
{
int dif = Mathf.Min(bullets, clipSize - clip);
bullets -= dif;
clip += dif;
reloading = false;
canShoot = true;
}
}
}
The important bit is this
int dif = Mathf.Min(bullets, clipSize - clip);
"dif" is the amout of bullets that need / can be added to our current clip. It takes whatever value is smaller, either the remaining bullets or the difference between the "missing" bullets in the clip. So it never adds more than it's needed to fill the clip and never more than there are remaining bullets left.
So if clip is 3 (so 7 bullets are "missing") but we only have 5 bullets left in "bullets" we only add those 5 bullets. So bullets becomes 0 and clip becomes "8".
Your answer
Follow this Question
Related Questions
How do I create a save scene feature though int value in c# 2 Answers
Sequential GameObject Activation 2 Answers
How to i set the int to hitpoints / 10 + the current int. 1 Answer
how can I print to text the number of an int as well as the name of the int. 1 Answer
How can I store HUGE NUMBERS on unity? (256bit integer) 0 Answers