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);
}
}
}