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");
}
}