using UnityEngine;
using System.Collections;
public class ZombieAttack : MonoBehaviour
{
public int zombieCol = 0;
float timer;
public float timeBetweenAttacks = 0.5f;
public int attackDamage;
HealthScript enemyHealth;
HealthCharacter playerHealth;
void Awake()
{
playerHealth = GetComponent<HealthCharacter>();
enemyHealth = GetComponent<HealthScript>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "Character")
{
zombieCol = 1;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.name == "Character")
{
zombieCol = 0;
}
}
void Update()
{
if (zombieCol == 1)
{
ZombieController move = GetComponent<ZombieController>();
move.playerSpeedForOtherScript = 0;
timer += Time.deltaTime;
if (timer > timeBetweenAttacks && enemyHealth.HP > 0)
{
Attack();
}
}
}
void Attack()
{
timer = 0f;
if (playerHealth.HP > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
}
using UnityEngine;
using System.Collections;
public class HealthCharacter : MonoBehaviour
{
public float HP;
public bool isEnemy = true;
public UISprite healthSprite;
public float attackZombieRadi;
public float timeBetweenAttack;
float timer;
public bool zombieAttack;
void Awake()
{
healthSprite.fillAmount = 1;
}
void Update()
{
timer += Time.deltaTime;
healthSprite.fillAmount = HP / 100;
if (HP <= 0)
{
healthSprite.fillAmount = 0f;
Destroy(gameObject);
}
}
public void TakeDamage (int amount)
{
HP -= amount;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "Character")
{
var pers = col.gameObject.GetComponent<HealthCharacter>();
if (pers != null)
{
pers.TakeDamage(attackDamage);
}
}
}