Reload Gun?

Hi, there. Can someone help me with an script… It is from a unity preset game. Is an shooter. How can i add reload function, and bullet count. I have the reload sound (3 sec duration).
Here is the script.
using UnityEngine;

I think unity does this on the c++ side, so no api. There probably are third party parsers for c#, but I never used one.

–

3 Answers

3

Add a int CurBullets,MaxPerClip and a ReservedAmmo

then at :
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) { Shoot (); }
make it :

if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) {
if(CurBullets > 0){
            Shoot ();
}else{
Reload();
}
        }

Then create a:

void Reload()
{
float shotsFired;

    shotsFired = BulletsPerClip - CurBullets;

    TotalBullets = CurBullets + reservedAmmo;
    if (TotalBullets < BulletsPerClip)
    {
        CurBullets = TotalBullets;
        isReloading = false;
        print("End Of Reload");
        TotalBullets = 0;

    }
    else
    {
        CurBullets = BulletsPerClip;
        reservedAmmo -= shotsFired;
        isReloading = false;
        print("End Of Reload");
        TotalBullets = 0;
    }
}

Then for waiting for the reload you need to make an IEnumerator waiting for the reload anim to end

you have to declare and initialize the new variables. at the top of your script in the variable declaration area, put (for example): public float BulletsPerClip = 30;

–

Ah yeah i missed the picture, i have edited that. Thanks for your answer but my pinguin is tagged as Player

–

public class AmmoBehaviour : MonoBehavior
{
public int ammo;
public int Maxammo;
public int Clipammo;
public int Clipsize;
int Rammo;
public bool canFire;
public bool isReloading;

// Update is called once per frame
void Update()
{
    if (Clipammo != 0 && isReloading == false)
    {
        canFire = true;
    }

    if (Input.GetKeyDown(KeyCode.Mouse0) && canFire && !this.tag.Contains("Auto"))
    {
        Clipammo--;
    }

    if (Input.GetKey(KeyCode.Mouse0) && canFire && this.tag.Contains("Auto"))
    {
        Clipammo--;
    }

    if (Input.GetKeyDown("r") && !isReloading)
    {
        isReloading = true;
        StartCoroutine(ReloadDelay());
    }

    if (Clipammo < 0) Clipammo = 0; canFire = false;
    if (ammo < 0) ammo = 0;

}

IEnumerator ReloadDelay()
{
    yield return new WaitForSeconds(3);
    if (ammo > Clipsize - Clipammo)
    {
        Rammo = Clipsize - Clipammo;
        Clipammo = Clipsize;
        ammo = ammo - Rammo;
    }
    else
    {
        Rammo = Clipsize - Clipammo;
        Clipammo = Clipammo + ammo;
        ammo = 0;
    }
    isReloading = false;
    canFire = true;
}

}

heres my code. Took a few hours of edits. It works for different types of guns. If the gun youre using is automatic and not semi automatic then make sure it includes the word “Auto” in its tag, then it will fire when LMB is held down, not when its clicked. Just set max ammo, ammo, clip ammo, and clip size to whatever you like, but keep rammo (remaining ammo) at 0. I also have a display script if you need it.