so, I’m new to Unity and I made a reload to my fps game, but the problem is when I select play mode
he keeps reloading forever even that I set the reload time to 2
here is my script, and hope someone helps
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public AudioSource ShootSound;
float timer = 0.0f;
float cooldownTime = 1.0f;
public float fireRate;
private float accuracy;
public AudioClip singleShot;
public int maxammo=10;
private int currentammo;
public float reloadtime= 3;
private bool isReloading= false;
public Camera fpscam;
void OnEnable(){
isReloading= false;
}
void Start()
{
maxammo=currentammo;
ShootSound= GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if(isReloading)
return;
if(currentammo <= 0)
{
StartCoroutine(Reload());
return;
}
if(timer > cooldownTime)
if(Input.GetMouseButton(0))
{
{
{
Shoot();
ShootSound.Play();
timer = 0;
}
}
}
if(timer < cooldownTime + 1)
timer += Time.deltaTime
;
IEnumerator Reload()
{
isReloading= true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(2);
currentammo= maxammo;
isReloading= false;
}
}
void Shoot(){
currentammo--;
RaycastHit hit;
if(Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target= hit.transform.GetComponent<Target>();
if(target!= null)
{
target.TakeDamage(damage);
}
}
}
}