Question by
siposferenc11 · Jul 08, 2020 at 06:20 AM ·
shootingbooleanammoreloading
Why did my boolean don't work?
So i watched a YT tutorial for ammo/reloading. Everything is fine but when i reload, i can shoot at the time.. i added booleans but it seems like nothing changed i still can shoot why reloading. Here's the script
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class lovesscript : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public Camera fps;
public int maxammo = 10;
public int currentammo;
private float reloadspeed = 1f;
public bool reloading = false;
public Animator animator;
public TextMeshProUGUI bullettext;
// Start is called before the first frame update
void Start()
{
currentammo = maxammo;
animator = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
bullettext.text = currentammo.ToString();
if(reloading)
return;
if(currentammo <= 0) {
StartCoroutine(nowreloading());
return;
}
if (Input.GetButtonDown("Fire1"))
Shooting();
void Shooting ()
{
currentammo--;
RaycastHit hit;
if (Physics.Raycast(fps.transform.position, fps.transform.forward, out hit, range))
{
UnityEngine.Debug.Log(hit.transform.name);
}
}
}
IEnumerator nowreloading() {
reloading = true;
UnityEngine.Debug.Log("Reloading");
animator.SetBool("needtoreload", true);
yield return new WaitForSeconds(reloadspeed);
animator.SetBool("needtoreload", false);
currentammo = maxammo;
reloading = false;
}
void OnEnable() {
reloading = false;
animator.SetBool("needtoreload", false);
}
}
Comment