Attack delay not working correctly
I have an enemy object that damages the player on collision. The script works correctly with no debug errors, however for some reason the attack delay is not working. I debug logged the timer to see if it was counting, which it was, so I am not really sure what the problem is!
Also, not a big issue but I may as well ask; Debug.Log shows that the enemy 'hits' the player once more after the player's health reaches zero (taking it into -x amount). Can't imagine this being a problem, but I am just curious why this may be happening.
Here is the code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyAttack : MonoBehaviour {
 
     public int enemyMaxHealth = 100; 
     public int enemyDamage = 10;
     public int enemyArmorRating = 10;
     public float enemyAttackDelay = 0.5f;
 
     private int enemyCurrentHealth;
     private float enemyAttackTimer;
 
     void Awake()
     {
         enemyCurrentHealth = enemyMaxHealth;
     }
 
     void Start()
     {
     }
 
     void Update()
     {
         enemyAttackTimer += Time.deltaTime;
     }
 
     public void DamageReceived(int gunDamageAmount)
     {
         enemyCurrentHealth -= gunDamageAmount - enemyArmorRating;
 
         if (enemyCurrentHealth <= 0f) 
         {
             gameObject.SetActive (false);
             Debug.Log ("Enemy killed!");
         }
     }
 
     void OnCollisionStay(Collision attack)
     {
         if (attack.gameObject.tag == "Player" && PlayerHealth.playerCurrentHealth >= 0 && enemyAttackTimer >= enemyAttackDelay)
         {
             PlayerHealth.playerCurrentHealth -= (enemyDamage - PlayerHealth.playerDefense);
             Debug.Log("You have been hit! Current Health: " + PlayerHealth.playerCurrentHealth);
         }
     }
 }
 
              Answer by ItzChris92 · May 04, 2017 at 09:50 PM
I basically used this here after many hours of searching, and trial and error on my code. Also fixed the second problem by changing the PlayerHealth.playerCurrentHealth >= 0 to PlayerHealth.playerCurrentHealth > 0. Easy fix for a simple mistake, as the collision was being carried out WHEN health = 0.
Your answer
 
             Follow this Question
Related Questions
why the Timer isn't working 0 Answers
How to make an object fall after a delay?,How to get the object to fall after a certain time 1 Answer
How to alter Right/Top Value from RectTransform 0 Answers
ScriptableObject with instance-attached script 0 Answers
Can someone please help me find out what wrong with my code. 1 Answer