Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

not losing ammo when shooting

$
0
0
i have a script that shots raycasts, and there's a function that reloads (3 seconds) and after those 3 seconds are up, the player has unlimited ammo for a short amount of time. The currentAmmo variable was not changing. I need to fix this. using System.Collections; using System.Collections.Generic; using UnityEngine; public class M4A1Gun : MonoBehaviour { public GameObject weepons; public Camera fpsCam; public ParticleSystem muzzleFlash; public GameObject impactEffect; public GameObject bloodHit; public Animator anim; public AudioSource shot; public AudioSource reloadSound; public float range = 100f; public float damage = 10f; public float fireRate = 15f; public int maxAmmo = 30; public int currentAmmo; public float reloadTime = 3f; private bool isReloading = false; private float nextTimeToFire = 0f; private float nextTimeToReload = 0f; // Use this for initialization void Start () { currentAmmo = maxAmmo; } // Update is called once per frame void Update () { if (weepons.GetComponent().selectedWeaopon == 0f) { Gun(); } if (isReloading == true && Time.time >= nextTimeToReload) { nextTimeToReload = Time.time + 15f / reloadTime; ReloadSound(); } } IEnumerator Reload () { isReloading = true; Debug.Log("Reloading"); anim.SetBool("reloading", true); this.GetComponent().shooting = false; yield return new WaitForSeconds(reloadTime); currentAmmo = maxAmmo; isReloading = false; anim.SetBool("reloading", false); } void ReloadSound() { reloadSound.Play(); } void Gun() { if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (isReloading) return; if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire) { nextTimeToFire = Time.time + 1f / fireRate; Shoot(); } if (Input.GetButtonUp("Fire1")) { this.GetComponent().shooting = false; } if (Input.GetButtonDown("Fire1")) { this.GetComponent().shooting = true; } } void Shoot () { muzzleFlash.Play(); shot.Play(); currentAmmo--; RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); WeebTarget target = hit.transform.GetComponent(); if (target != null) { target.TakeDamage(damage); } GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 2f); if (hit.rigidbody != null) { GameObject bloodGO = Instantiate(bloodHit, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(bloodGO, 2f); } } } }

Viewing all articles
Browse latest Browse all 592

Trending Articles