Quantcast
Viewing all articles
Browse latest Browse all 1488

Rapid fire does damage every frame

I have a script for Rapid fire. Apparently it damages enemy each frame. But I want the damage to happen ONLY when a bullet is fired. Here are the enemy script, gun fire script and gun damage script. Basically I send a message to the enemy script's DeductPoints function from Gun Damage script, in order to deduct health from the enemy. This works perfectly in single fire guns. But in rapid fire, this function is overly called. Any idea how to fix this? using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyScript : MonoBehaviour { public int enemyHealth; public GameObject zombie; // Use this for initialization void Start () { enemyHealth = 50; } // Update is called once per frame void Update () { if (enemyHealth <= 0) { this.GetComponent().enabled = false; zombie.GetComponent().Play("Dying"); StartCoroutine(EndZombie()); } } void DeductPoints(int damageAmount) { Debug.Log(enemyHealth + " " + damageAmount); enemyHealth -= damageAmount; } IEnumerator EndZombie() { yield return new WaitForSeconds(3); Destroy(gameObject); } } Gunfire script using System.Collections; using System.Collections.Generic; using UnityEngine; public class GunFireMP5K : MonoBehaviour { public GameObject mp5k; public AudioSource mp5kSound; public GameObject muzzleFlash; public static int ammoCount; public static bool firing; public GameObject upCursor; public GameObject downCursor; public GameObject leftCursor; public GameObject rightCursor; // Use this for initialization void Start () { } // Update is called once per frame void FixedUpdate () { ammoCount = GlobalAmmo.loadedAmmo; if (Input.GetButton("Fire1")) { if (ammoCount >= 1) { if (!firing) { StartCoroutine(MP5KFire()); } } } } IEnumerator MP5KFire() { firing = true; upCursor.GetComponent().enabled = true; downCursor.GetComponent().enabled = true; leftCursor.GetComponent().enabled = true; rightCursor.GetComponent().enabled = true; GlobalAmmo.loadedAmmo -= 1; //zombie.SendMessage("DeductPoints",10); mp5kSound.Play(); muzzleFlash.SetActive(true); mp5k.GetComponent().enabled = true; yield return new WaitForSeconds(0.1f); muzzleFlash.SetActive(false); mp5k.GetComponent().enabled = false; upCursor.GetComponent().enabled = false; downCursor.GetComponent().enabled = false; leftCursor.GetComponent().enabled = false; rightCursor.GetComponent().enabled = false; firing = false; } } Gun damage script using System.Collections; using System.Collections.Generic; using UnityEngine; public class MP5KDamage : MonoBehaviour { public int damageAmount; public float targetDistance; public float allowedRange; RaycastHit shot; RaycastHit hit; public GameObject blood; public GameObject bloodGreen; public static float shotsFired; public static float shotsHit; // Use this for initialization void Start () { allowedRange = 10.0f; damageAmount = 10; shotsFired = 0; shotsHit = 0; } // Update is called once per frame void FixedUpdate() { if (GlobalAmmo.loadedAmmo >= 1) { if (Input.GetButton("Fire1")) { if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out shot)) { shotsFired = shotsFired + 1; targetDistance = shot.distance; if (targetDistance < allowedRange) { if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward),out hit)) { if (hit.transform.tag == "Zombie") { shotsHit = shotsHit + 1; Instantiate(blood, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); } if (hit.collider.tag == "ZombieHead") { shotsHit = shotsHit + 1; damageAmount = 10; Instantiate(blood, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); } if (hit.transform.tag == "Spider") { shotsHit = shotsHit + 1; Instantiate(bloodGreen, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); } } shot.transform.SendMessage("DeductPoints", damageAmount, SendMessageOptions.DontRequireReceiver); damageAmount = 10; } } } } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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