First gun script need advice for gun magazine

Hello all

First of all I apologize up front for posting such a long code.
I am truly sorry for it, but i have been struggling with for a while now.

My goal is to create a gun script, where I have a panel showing the numbers of bullet for current magazine.
There should maximum be 30 bullets in a magazine.
Player can maximum have 3 magazine clips.

This is the first script I have written by my self, no doubt there must be many easier ways, but I ended up hardcoding everything.

I have the panel and the UI text for the bullets. I have 3 images of clips.
What I cant seem to figure out, is:

  • When bullets are empty from the first clip, how to switch over to a second clip?
  • When pressing “R” (reload) it should fill up taking bullets from available clip
public float reloadTime = 1.5f;


private int maxAmmo = 30;
private int maxClips = 3;

//.....................
private int currentAmmo;
private int currentClip;
//......................

bool IsReLoading = false;
bool clipEmpty;
bool clipsFull;
bool allClipsEmpty;

bool clip1Full;
bool clip2Full;
bool clip3Full;

int clipsLeft;

public Image clipMag1;
public Image clipMag2;
public Image clipMag3;

public int clipAmmo1 = 0;
public int clipAmmo2 = 0;
public int clipAmmo3 = 0;


private void  Start()
{
   //DISBALED IN UPDATE......
  
   clipMag1 = GetComponent<Image>();
   clipMag2 = GetComponent<Image>();
   clipMag3 = GetComponent<Image>();
  
   allClipsEmpty = true;

}



// SCENARIO AFTER PICKING UP FIRST MAGAZINE


private void OnTriggerEnter2D(Collider col)
{

           if(col.tag == "Player")
           {
                if( allClipsEmpty)
                {
                    clipMag1.enabled = true;
                    clipAmmo1 = maxAmmo;
                   
                    clip1Full = true;
                    clip2Full = false;
                    clip3Full = false;
               
                }
               
                if( clip1Full = true  )
                {
                  
                    clipMag2.enabled = true;
                    clipAmmo2 = maxAmmo;
                   
                    clip2Full = true;
                    clip3Full = false;
               
                }
               
                if( clip1Full == true && clip2Full = true  )
                {
                    clipMag3.enabled = true;
                    clipAmmo3 = maxAmmo;
                   
                    clip3Full = true;
                   
                    clipsFull = true;
                   
               
                }
               
               
                else if(clipsFull)
                {
               
               
                Debug.Log("AMMO IS FULL");
               
                }
               
               
           }

}







void Update()
{


// Shoot

         if(Input.GetButtonDown(Keycode("Fire1"))
         {
        
                  //Shoot
   
         }




     if(allClipsEmpty == true)
     {
             clipMag1.enabled = false;
             clipMag2.enabled = false;
             clipMag3.enabled = false;
     }
    
    
     if(IsReLoading)
         return;
    
           if(Input.GetKeyDown(KeyCode.R))
           {
          
           if(!allClipsEmpty)
          
               {
          
           StartCourotine(ReLoading());
           return;
          
               }
          
           }
           else
           {
              Debug.Log("NO CLIPS");
           }

              

      }

}

    

IEnumerator ReLoading()
{
     IsReLoading = true;
     Debug.Log("RELOADING");
    
    
     //FILL CLIP 3 AMMO
     if(clipAmmo1 >= 30 && clipAmmo2 >=30 )
     {
         clipAmmo3 = maxAmmo;
        
         clipMag1.enabled = true;
         clipMag1.enabled = true;
         clipMag1.enabled = true;
        
         bool clip1Full = true;
         bool clip2Full = true;
         bool clip3Full = true;
     }
     //FILL CLIP 2 AMMO
     if(clipAmmo3 <=0   && clipAmmo1 >=30 )
     {
         clipAmmo2 = maxAmmo;
        
         clipMag1.enabled = true;
         clipMag1.enabled = true;
         clipMag3.enabled = false;
        
         bool clip1Full = true;
         bool clip2Full = true;
         bool clip3Full = false;
     }
    
     //FILL CLIP 1 AMMO
      if(clipAmmo3 <=0   && clipAmmo2 <=0 )
     {
         clipAmmo1 = maxAmmo;
        
         clipMag1.enabled = true;
         clipMag1.enabled = false;
         clipMag3.enabled = false;
        
         bool clip1Full = true;
         bool clip2Full = false;
         bool clip3Full = false;
     }
    
    
    

     yield return new WaitForSeconds(reloadTime);
    
     // After above specified seconds = reloadTime then ammo will be filled update
     
     
      IsReLoading = false;
      

     
}

Do you care that it is broken into 3 magazines? If you do not, then just have a single number for how many bullets remaining:

int BulletsInPocket;

Then have a counter for how many are in the gun:

int BulletsInMagazine;

and a maximum:

const int MaxMagazineBullets = 30;  // unfortunately not in California

On reload:

  • calculate how many more can be fit into the magazine
  • make sure it is less than or equal to BulletsInPocket
  • subtract them from BulletsInPocket
  • add them to the BulletsInMagazine

If you want to track on a per-magazine basis, that is a LOT more work. Go check out some “stackable inventory” tutorials for ideas for organizing that. It’s probably 10x more complicated, if not more.

1 Like

Like Kurt said. Does magazine 1 need to be empty before they can use magazine 2? So you know if magazine 2 is empty, then so is magazine 1, etc. Do you have your game working as you expect with just one magazine, and now you want to add more magazines? Build on success!

No I don’t mind it’s broken into 3.

But I do have 3 clip images. When one round/magazine is empty I will disable the clip image for the empty.

So if I want to maximum have 3 clips that’s 90 bullets. So I guess that when I get below 30 bullets I can disable a ui image.

wait… So if I have full 3 clips magazine, 90 bullets then I could use if statement to control when to disable the 3 clips right??

I want to make it easy on my self, so you have to empty a magazine before you can reload and fill up.

Make is easy on yourself to start, just use one clip. Get that working. Then make 3.

2 Likes

Thank you Jeff.
Good advice

I’m not really addressing the actual question, but if your goal is for realism most firearms will chamber a round before the user chooses to fire. So actual capacity of the firearm is “magazine_capacity +1”. So an empty firearm, where you insert a full 30 round magazine and chamber a round, you’ll have 1 in the chamber and 29 in the magazine. If the user changes magazines before the firearm is empty, you’ll then have 1 in the chamber and 30 in the magazine, for a total capacity of 31.

Games which are going for max realism (Escape from Tarkov for example, among others) will replicate this behavior. Games which aren’t going for the realism factor usually do not bother with this.

1 Like