Hey,
So I need your help. I’m not that good at maths also I don’t know if there is maths needed but let me describe what I want to do:
When I want to reload and I have, let’s say 20/7 ammo and max. clip size is 30. When I want to reload it,
I decrease the total ammo I have which is 7 in this case by the distinction of the max clip size and the current clip size so:
totalAmmo -= maxClipSize - currentClipSize
so 7 - (30 - 20) = -3… I want it to decrease the totalAmmo (7) always to 0 if the totalAmmo after this action would be a negative number and fill my currentClipSize (20) by 7 in this situation. How to do it? I was thinking for so long but I can’t figure it out.
Hi @Aspect13, you were very close to a correct answer. You needed some brackets. I made an example snippet with reload wait times that I would use when doing reloading/ Cooldown. Hope this helps.
public float reloadTime;
public int clipSize;
public int remainingClip;
public int totalAmmo;
void FixedUpdate () {
// Primary Fire
if (input.KetKeyDown (KeyCode.MouseButton0))
Fire();
if (input.KetKeyDown (KeyCode.R))
StartReload();
}
void Fire () {
// Do normal fire mechanics
// Check if reload neccesary
if (remainingClip == 0)
StartReload ();
}
void StartReload () {
// Start Any Animations Here if required
/* Animation Code Here */
// Start any sound effects here
/* Sound Effect Code Here */
// Begin the count down
StartCoroutine (Reloading ());
}
IEnumerator Reloading () {
// Wait for the required time
yield return new WaitForSeconds(reloadTime);
// finish the reloading secquence and update any variables
FinnishReoad ();
}
void FinnishReload () {
// Do the reload calculations
if (totalAmmo < clipSize - remainingClip) {
remainingClip += totalAmmo;
totalAmmo = 0;
} else if (totalAmmo >= clipSize - remainingClip) {
totalAmmo -= (clipSize - remainingClip);
remainingClip = clipSize;
}
}