using System.Collections;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 1f;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
public Animator animator;
public Camera fpsCam;
void Start()
{
currentAmmo = maxAmmo;
}
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
if(Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(Reload());
return;
}
}
if (Input.GetMouseButton(0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f/fireRate;
shoot();
}
}
IEnumerator Reload ()
{
isReloading = true;
Debug.Log(“Reloading…”);
animator.SetBool(“Reloading”, true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool(“Reloading”, false);
yield return new WaitForSeconds(.25f);
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();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
hello. this is my gun script from brackeys yt and i wanna make it so when no ammo is true i want it to make the player have to hit r to reload other than just do it automaticaly