Created
October 28, 2017 22:05
-
-
Save JonathanYin/001324553de8ffbc13cee1dc819a18ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class EnemyAttack : MonoBehaviour | |
{ | |
public float timeBetweenAttacks = 0.5f; | |
public int attackDamage = 10; | |
Animator anim; | |
GameObject player; | |
PlayerHealth playerHealth; | |
EnemyHealth enemyHealth; | |
bool playerInRange; | |
float timer; | |
void Awake () | |
{ | |
player = GameObject.FindGameObjectWithTag ("Player"); | |
playerHealth = player.GetComponent <PlayerHealth> (); | |
enemyHealth = GetComponent<EnemyHealth>(); | |
anim = GetComponent <Animator> (); | |
} | |
void OnTriggerEnter (Collider other) | |
{ | |
if(other.gameObject == player) | |
{ | |
playerInRange = true; | |
} | |
} | |
void OnTriggerExit (Collider other) | |
{ | |
if(other.gameObject == player) | |
{ | |
playerInRange = false; | |
} | |
} | |
void Update () | |
{ | |
timer += Time.deltaTime; | |
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0) | |
{ | |
Attack (); | |
} | |
if(playerHealth.currentHealth <= 0) | |
{ | |
anim.SetTrigger ("PlayerDead"); | |
} | |
} | |
void Attack () | |
{ | |
timer = 0f; | |
if(playerHealth.currentHealth > 0) | |
{ | |
playerHealth.TakeDamage (attackDamage); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment