Example #1
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R) && currentAmmo < maxAmmo) //press r when current ammo is not max ammo to reload
        {
            StartCoroutine(Reload());
        }
        if (Input.GetButtonUp("Fire1")) // make sure muzzle flash dont play when not firing
        {
            muzzleFlash.Clear();
            muzzleFlash.Stop();
        }
        if (currentAmmo <= 0)
        {
            muzzleFlash.Clear(); // no muzzle flash when gun have no ammo.
            muzzleFlash.Stop();
        }

        if (isReloading)
        {
            return;
        }
        if (currentAmmo <= 0)
        {
            StartCoroutine(Reload()); //can't call IEnumerator functions normally
            return;
        }

        if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
            Debug.Log("Shoot");
        }
        gunInfo.ShowAmmo(currentAmmo, maxAmmo);
    }