- Home /
 
               Question by 
               zed787 · Feb 27 at 12:48 PM · 
                c#weaponweapon system  
              
 
              How do I stop my weapon from firing while reloading?
there is a bug to my script that if I spam clicks, my weapon can still fire while reloading any help? or anything I can do to fix this?
my Code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Shotgunscript : MonoBehaviour
 {
     public int damage = 50;
     public int ammoleft = 1;
     private int maxammo = 1;
     private float realoadtime = 2f;
     private bool isReloading = false;
     private bool readytofire = true;
     public float range = 100f;
     public Camera Camera;
     public ParticleSystem muzzleflash;
 
 
     private void Start()
     {
         ammoleft = maxammo;
     }
     private void Update()
     {
         if (Input.GetButtonDown("Fire1") && readytofire == true)
         {
             shoot(); 
         }
 
         if(ammoleft <= 0)
         {
             StartCoroutine(Reload());
             return;
         }
 
         if (isReloading)
         {
             return;
         }
     }
 
     private void shoot()
     {
         RaycastHit hit;
         RaycastHit hit_1;
         RaycastHit hit_2;
         RaycastHit hit_3;
 
         ammoleft--;
         muzzleflash.Play();
         if (Physics.Raycast(Camera.transform.position, Camera.transform.forward,out hit, range))
         {
             Debug.Log(hit.transform.name);
             muzzleflash.Play();
         }
 
         if (Physics.Raycast(Camera.transform.position, Camera.transform.forward, out hit_1, range))
         {
             Debug.Log(hit_1.transform.name);
             muzzleflash.Play();
         }
 
         if (Physics.Raycast(Camera.transform.position, Camera.transform.forward, out hit_2, range))
         {
             Debug.Log(hit_2.transform.name);
             muzzleflash.Play();
         }
 
         if (Physics.Raycast(Camera.transform.position, Camera.transform.forward, out hit_3, range))
         {
             Debug.Log(hit_3.transform.name);
             muzzleflash.Play();
         }
         readytofire = false;
              
     }
 
     private IEnumerator Reload()
     {
         isReloading = true;
         Debug.Log("Reloading");
 
         yield return new WaitForSeconds(realoadtime);
 
         ammoleft = maxammo;
         readytofire = true;
         Debug.Log("reloaded_finshed");
         isReloading = false;
     }
 
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by V_0_1_D_ · Feb 27 at 01:36 PM
Move if (isReloading){return;} to the first line of Void Update{}.
Answer by JdeHaan · Feb 27 at 01:37 PM
It might be fixed by changing this
    if (Input.GetButtonDown("Fire1") && readytofire == true)
              {
                  shoot(); 
              }
to
 if (Input.GetButtonDown("Fire1") && readytofire == true && isReloading == false)
          {
              shoot(); 
          }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A few problems with my weapon wheel script 1 Answer
Distribute terrain in zones 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                