- Home /
Reload Ammo is not working
Hello Im 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 dont consume ammo for 2 seconds 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");
}
}
Comment
Your answer
Follow this Question
Related Questions
Reload Ammo is not working 1 Answer
adding a delay to shooting 2 Answers
Invoke shoot delay problem. 3 Answers
How do I make my bullet tracers show when shooting in the air? 0 Answers
How do I make bullet tracers show when I shoot in the air? 1 Answer