Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Adding first 12 bullets from an ammo reserve to magazine problem

Discussion in 'Scripting' started by solidp26, Jan 31, 2021.

  1. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    The ammo system in my game consists of: currentAmmo (ammount of bullets currently in a magazine), maxCurrentAmmo =12, reserveAmmo and maxReserveAmmo.

    Currently the reload function works like this:

    Code (CSharp):
    1. IEnumerator Reload()
    2.     {
    3.  
    4.         anim.SetTrigger("reload");
    5.         yield return new WaitForSeconds(reloadTime);
    6.  
    7.         reserveAmmo -= maxCurrentAmmo - currentAmmo;
    8.         currentAmmo = maxCurrentAmmo;
    9.  
    10.         canShoot = true;
    11.     }
    It obviousely works fine, but the problem is when the reserveAmmo becomes less than maxCurrentAmmo it just doesn't make any sense to set currentAmmo to 12 when there is not enough reserve ammo to do so.

    How i want it to work is to add first 12 bullets from reserveAmmo to currentAmmo.

    For example: maxCurrentAmmo = 12, reserveAmmo = 3 and current ammo = 4. When the reload function is called it should subtract reserveAmmo and sum it with currentAmmo. But at the same time make sure it wont go over maxCurrentAmmo
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Check if there is still a full magazin worth of reserve ammo. If so, fill it up, otherwise use the rest of reserve.
    Code (CSharp):
    1. currentAmmo = reserveAmmo >= maxCurrentAmmo ? maxCurrentAmmo : reserveAmmo;
    You should make sure above that reserveAmmo cannot become negative tho. You can do that with a tertiary operator like here as well.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Just take it step by step.

    • First, subtract current ammo from maxCurrentAmmo. Store that result in an intermediate variable called
      desiredFromReserve
      . That's the amount of bullets you want to take from your reserve.
    • Next, use
      System.Math.Min(desiredFromReserve, reserveAmmo)
      and store that in another variable called
      actualReloadAmount
      . That's the actual amount you will pull from reserve, which is the minimum of what you want and what you actually have available.
    • Finally just add
      actualReloadAmount
      to
      maxCurrentAmmo
      , and subtract
      actualReloadAmount
      from
      reserveAmmo
      .
    You should probably also check if
    actualReloadAmount
    is greater than 0 before doing the animation. If it's not, you shouldn't play the reload animation, because there's nothing to reload.
     
    solidp26 likes this.
  4. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    Thanks! This method works exactly as i wanted!