Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arcane94 · Nov 18, 2018 at 10:13 AM · enemyshootingdamage

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<ZombieFollow>().enabled = false;
             zombie.GetComponent<Animation>().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<Animator>().enabled = true;
         downCursor.GetComponent<Animator>().enabled = true;
         leftCursor.GetComponent<Animator>().enabled = true;
         rightCursor.GetComponent<Animator>().enabled = true;
         GlobalAmmo.loadedAmmo -= 1;        
         //zombie.SendMessage("DeductPoints",10);
         mp5kSound.Play();
         muzzleFlash.SetActive(true);
         mp5k.GetComponent<Animator>().enabled = true;
         
         yield return new WaitForSeconds(0.1f);
         
         muzzleFlash.SetActive(false);
         mp5k.GetComponent<Animator>().enabled = false;
         upCursor.GetComponent<Animator>().enabled = false;
         downCursor.GetComponent<Animator>().enabled = false;
         leftCursor.GetComponent<Animator>().enabled = false;
         rightCursor.GetComponent<Animator>().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;
                     }
                 }
             }
         }
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies sharing health 2 Answers

Shooting and damage with raycast doesnt work. 0 Answers

Enemy wont take Damage. How could i fix this?,Enemy wont take damage with raycast 1 Answer

Staged bullet damage? 1 Answer

Please, I need help, I'm trying to create a Coroutine where my enemy shoots 1 pack of 4 bullets, every 2 seconds, but it just ended up in the enemy shooting some bullets and then never again 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges