Gun - Reloading , ammo and clips

Hello.

I have a gun script with reloading,shooting and recoil.

My problem is when the player presses the R ( reloading ) key the number of clips the player is carying gets to zero.I want the clips to get lowered by 1.

Anybody knows what might be the problem ?

This is my code.
Feel free to use whatever you want.

    [Header("Atributes")]
    public float fireRate = 0.25f;
    public float projektileSpeed = 100f;
    public float damage = 10f;

    [Header("Ammo")]
    public int maxAmmo;
    public int currentAmmo;
    public int clips;

    [Header("Objects and Transforms")]
    public Projektil projektil;
    public Transform muzzle;

    [Header("Animations")]
    public AnimationClip reloadingClip;
    Animator animator;
    HracPOHYB hrcPohyb;

    bool isReloading;
    bool isScoped;
    bool isWalking;
    bool isRunning;
    bool canShoot;

    [Header("Recoil")]
    public Vector2 kickMinMax = new Vector2(.05f, .2f);
    public Vector2 recoilAngleMinMax = new Vector2(3, 5);
    public float recoilMoveSettleTime = .1f;
    public float recoilRotationSettleTime = .1f;

    Vector3 recoilSmoothDampVelocity;
    float recoilRotSmoothDampVelocity;
    float recoilAngle;

    float nextFire;

    private void Start()
    {
        animator = GetComponentInChildren<Animator>();
        currentAmmo = maxAmmo;
        hrcPohyb = GetComponentInParent<HracPOHYB>();

        if(currentAmmo > 0)
        {
            canShoot = true;
        }
    }

    private void Update()
    {

        //Reloading Animations
        if (Input.GetKey(KeyCode.R))
        {
            isReloading = true;
        }
       

        if (isReloading)
        {
            if(clips > 0)
            {
                StartCoroutine(Reloading());
              
            }
        }

        //Aiming Animations//
        if (Input.GetButtonDown("Fire2"))
        {
            isScoped = !isScoped;
            animator.SetBool("SCOPED", isScoped);
        }

        //Movement Animations//
        if (hrcPohyb.currentSpeed >= 1 && hrcPohyb.currentSpeed <= 10)
        {
            animator.SetBool("WALKING", true);
            animator.SetBool("SPRINTING", false);
        }
        else if(hrcPohyb.currentSpeed >= 10)
        {
            animator.SetBool("WALKING", false);
            animator.SetBool("SPRINTING", true);
        }
        else if (hrcPohyb.currentSpeed == 0)
        {
            animator.SetBool("WALKING", false);
            animator.SetBool("SPRINTING", false);
        }

        //Shooting/Recoil and Ammo//
        if (Input.GetMouseButton(0) && Time.time > nextFire && canShoot)
        {
            if(currentAmmo > 0)
            {
                Shoot();
            }
          
        }

        transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, recoilMoveSettleTime);
        recoilAngle = Mathf.SmoothDamp(recoilAngle, 0, ref recoilRotSmoothDampVelocity, recoilRotationSettleTime);
        transform.localEulerAngles = Vector3.left * recoilAngle;
    }

  

    void Shoot()
    {
        nextFire = Time.time + fireRate;

        Projektil newProjectile = Instantiate(projektil, muzzle.position, muzzle.rotation) as Projektil;
        currentAmmo--;

        newProjectile.SetSpeed(projektileSpeed);
        newProjectile.SetDamage(damage);

        transform.localPosition -= Vector3.forward * Random.Range(kickMinMax.x, kickMinMax.y);
        recoilAngle += Random.Range(recoilAngleMinMax.x, recoilAngleMinMax.y);
        recoilAngle = Mathf.Clamp(recoilAngle, 0, 30);
    }

    IEnumerator Reloading()
    {
        canShoot = false;
       

        animator.SetBool("RELOADING", true);
        animator.SetBool("SCOPED", false);

        currentAmmo = 0;
        clips--;

        yield return new WaitForSeconds(reloadingClip.length);

        currentAmmo = maxAmmo;

        animator.SetBool("RELOADING", false);

        isReloading = false;
        canShoot = true;

    }

}

You are using GetKey, which works like autofire. You probably want GetKeyDown.

you should have it in the if clause:

if(input.GetKeyDown(whatever) && !isReloading){
isReloading = true;
startCoroutine(Reloading());
}
1 Like

You’re starting the Reloading coroutine from the Update function, which means it gets executed every frame as long as isReloading is active. Since the first thing the coroutine does is set currentAmmo to 0, it basically does just that every single frame.

Suggest you add a simple boolean check to the coroutine - something like this:

IEnumerator Reloading()
{
if (!isReloading)
{
// do things
}

else { // do nothing}
}
1 Like

Didn’t think of that.Thank you :smile: