Quantcast
Channel: Questions in topic: "left"
Viewing all articles
Browse latest Browse all 135

Bullet Counter Not Working Propperly

$
0
0
hey guys i have 2 scripts that handle my weapon i have a problem and dont exactly know how to fix it the problem is in my player_shooting.... Script.... my ammo left is goint faster than my bulletrs that i fire but when i set it to getbuttonDown (Single Shot ).. its fine but this is for a fast firing gun can anyone help me fix this here are the scripts.... **Thanks!** -------------------// using UnityEngine; using System.Collections; using UnityEngine.UI; public class Player_Shooting_Weapon : MonoBehaviour { float cooldown = 0; public GameObject muzzleFlash; FX_Manager fxManager; WeaponData weaponData; Animator anim; public int maxAmmo = 0; public int AmmoLeft = 10; private int currentAmmo; public float reloadTime = 1f; private bool isReloading = false; public AudioSource audioSource; public AudioClip ReloadSoundFx; public Text AmmoLeftText; private void Start() { if (currentAmmo == -1f) currentAmmo = (int)maxAmmo; fxManager = GameObject.FindObjectOfType(); if(fxManager == null) { Debug.Log("Couldn`t find an FX_Manager"); } } IEnumerator Reload() { isReloading = true; muzzleFlash.SetActive(false); audioSource.PlayOneShot(ReloadSoundFx, 0.7f); // Anima.SetBool("Reloading",true) Debug.Log("Reloading...."); yield return new WaitForSeconds(reloadTime - .25f); // Anima.SetBool("Reloading",false) yield return new WaitForSeconds(.25f); currentAmmo = (int)maxAmmo; isReloading = false; } // Update is called once per frame void Update() { if (AmmoLeftText != null) { AmmoLeftText.text = AmmoLeft.ToString(); } if (isReloading) return; if (AmmoLeft <= 0) { muzzleFlash.SetActive(false); return; } if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (Input.GetMouseButton(0)) { // Player wants to shoot...so. Shoot. Fire(); } if (Input.GetMouseButtonUp(0)) { muzzleFlash.SetActive(false); } cooldown -= Time.deltaTime; } void OnEnable() { isReloading = false; // Anima.SetBool("Reloading",false) } void Fire() { AmmoLeft--; currentAmmo--; if (weaponData == null) { weaponData = gameObject.GetComponentInChildren(); if (weaponData == null) { Debug.Log("Did not find any WeaponData in our children!"); return; } } //muzzleFlash.SetActive(true); if (cooldown > 0) { return; } muzzleFlash.SetActive(true); Debug.Log("Firing our gun!"); Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); Transform hitTransform; Vector3 hitPoint; hitTransform = FindClosestHitObject(ray, out hitPoint); if (hitTransform != null) { Debug.Log("We hit: " + hitTransform.name); // We could do a special effect at the hit location // DoRicochetEffectAt( hitPoint ); Health h = hitTransform.GetComponent(); while (h == null && hitTransform.parent) { hitTransform = hitTransform.parent; h = hitTransform.GetComponent(); } // Once we reach here, hitTransform may not be the hitTransform we started with! if (h != null) { // This next line is the equivalent of calling: // h.TakeDamage( damage ); // Except more "networky" PhotonView pv = h.GetComponent(); if (pv == null) { Debug.LogError("Freak out!"); } else { h.GetComponent().RPC("TakeDamage", PhotonTargets.AllViaServer, weaponData.damage); } } if (fxManager != null) { muzzleFlash.SetActive(true); DoGunFX(hitPoint); } } else { // We didn't hit anything (except empty space), but let's do a visual FX anyway if (fxManager != null) { muzzleFlash.SetActive(true); hitPoint = Camera.main.transform.position + (Camera.main.transform.forward * 100f); DoGunFX(hitPoint); } } cooldown = weaponData.fireRate; } void DoGunFX(Vector3 hitPoint) { WeaponData weaponData = gameObject.GetComponentInChildren(); fxManager.GetComponent().RPC("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint); } Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) { RaycastHit[] hits = Physics.RaycastAll(ray); Transform closestHit = null; float distance = 0; hitPoint = Vector3.zero; foreach (RaycastHit hit in hits) { if (hit.transform != this.transform && (closestHit == null || hit.distance < distance)) { // We have hit something that is: // a) not us // b) the first thing we hit (that is not us) // c) or, if not b, is at least closer than the previous closest thing closestHit = hit.transform; distance = hit.distance; hitPoint = hit.point; } } // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit return closestHit; } } --------------------------------------------------------------- using System.Collections; using System.Collections.Generic; using UnityEngine; public class WeaponData : MonoBehaviour { public float fireRate = 0.1f; public float damage = 25f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }

Viewing all articles
Browse latest Browse all 135

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>