- Home /
Reload Ammo is not working
Hello, I'm trying to write my own shooting script but when I try to add reload time to my script it reloads in time I want(2 seconds) but it also don't consume ammo for 2 seconds after reload. Can you guys help me to fix this issue ?
 {
     public Camera Cam;
     public Animator anim;
     public Text ammoText;
     public Text reloadText;
     public float reloadSpeed = 2f;
     public float DefaultMag = 2;
     public float range = 150f;
     public float Force = 100f;
     [SerializeField]
     private float CurrentMag;
     bool needReload;
     public void Start()
     {
         CurrentMag = DefaultMag;
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0) && CurrentMag > 0)
         {
             CurrentMag--;
             Shoot();
         }
         else
             anim.SetBool("Shoot", false);
 
         if (CurrentMag <= 0 || Input.GetKeyDown(KeyCode.R) && CurrentMag != DefaultMag)
         {
             needReload = true;
             Debug.Log("Reloading");
             reloadText.text = "Reloading";
             StartCoroutine(Reload());
         }
         else
         {
             anim.SetBool("Reload", false);
             reloadText.text = "OwO";
         }
         ammoText.text = CurrentMag + "/" + DefaultMag;
     }
 
     private IEnumerator Reload()
     {
         yield return new WaitForSeconds(reloadSpeed);
         anim.SetBool("Reload", true);
         CurrentMag = DefaultMag;
         Debug.Log("Reloaded");
         needReload = false;
     }
     void Shoot()
     {
         
         RaycastHit hit;
         anim.SetBool("Shoot", true);
         Debug.Log("Shooted");
 
         if(Physics.Raycast(Cam.transform.position, Cam.transform.forward, out hit, range))
         {
             //Destroy(hit.transform.gameObject);
             hit.rigidbody.AddForceAtPosition(transform.forward * Force, hit.point);
         }
             else
                 Debug.Log("miss");
     }
 }
               Comment
              
 
               
              Answer by Ky6eH · Aug 20, 2020 at 09:40 AM
@aslanulascan22 try this code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ammoReaload : MonoBehaviour
 {
     public Camera Cam;
     public Animator anim;
     public Text ammoText;
     public Text reloadText;
     public float reloadSpeed = 2f;
     public float DefaultMag = 2;
     public float range = 150f;
     public float Force = 100f;
     [SerializeField]
     private float CurrentMag = DefaultMag;
     bool needReload = false;
 
     public bool isReloading = false; //new var too check is weapon still reload
 
     private void Update()
     {
         if (isReloading) //stop Update while reloading
             return;
 
         if (Input.GetMouseButtonDown(0) && CurrentMag > 0)
         {
             CurrentMag--;
             Shoot();
         }
 
         if (CurrentMag <= 0 || Input.GetKeyDown(KeyCode.R) && CurrentMag != DefaultMag)
         {
             needReload = true;
             Debug.Log("Reloading");
             reloadText.text = "Reloading";
             StartCoroutine(Reload());
         }
         ammoText.text = CurrentMag + "/" + DefaultMag;
     }
 
     private IEnumerator Reload()
     {
         isReloading = true; // turn off Update after delay
         anim.SetBool("Reload", true); // start animation before delay
         yield return new WaitForSeconds(reloadSpeed);
         anim.SetBool("Reload", false); // stop animation after delay
         isReloading = false; // turn on Update after delay
         CurrentMag = DefaultMag;
         Debug.Log("Reloaded");
         needReload = false;
     }
 
     void Shoot()
     {
         RaycastHit hit;
         anim.SetBool("Shoot", true);
         Debug.Log("Shooted");
         anim.SetBool("Shoot", false);
         if (Physics.Raycast(Cam.transform.position, Cam.transform.forward, out hit, range))
         {
             //Destroy(hit.transform.gameObject);
             hit.rigidbody.AddForceAtPosition(transform.forward * Force, hit.point);
         }
         else
             Debug.Log("miss");
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Reload Ammo is not working 0 Answers
adding a delay to shooting 2 Answers
Gun Shooting Animation Won't Play 0 Answers
Reloading Script 2 Answers
Gun reload and clip size 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                