How to create a timer for reload?
Hello, I’m currently making an FPS game, and I’m trying to create a reload function to reload the guns, but I can’t find a suitable solution on the documentation or unity answers. I found a possible one on unity answers, but that one had extra variables, so I didn’t know which to include... Can anyone help me?
What I currently have...
//Reload Primary
if (Input.GetKeyDown(KeyCode.R) && primAmmoReserve >= 0 && holdingPrimary == true)
{
ammoUI.text = "Reloading...";
//Wait 3 seconds
ammoUI.text = "Ammo: " + primAmmo + "/" + primAmmoReserve;
primAmmo = primAmmo + primAmmoUsed;
primAmmoReserve = primAmmoReserve - primAmmoUsed;
primAmmoUsed = 0;
}
Comment
Answer by markythemurloc · Jun 23, 2020 at 09:22 PM
Hey!
You could you an IEnumerator.
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
//Call the IEnumerator function.
StartCoroutine(ReloadDelay());
}
}
private IEnumerator ReloadDelay()
{
//After 2 seconds do whatever you want.
yield return new WaitForSeconds(2f);
enableFire = true;
}