Guest User

Untitled

a guest
Aug 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyAttack : MonoBehaviour
  5. {
  6. public float timeBetweenAttacks = 0.5f;
  7. public int attackDamage = 10;
  8.  
  9.  
  10. Animator anim;
  11. GameObject player;
  12. PlayerHealth playerHealth;
  13. EnemyHealth enemyHealth;
  14. bool playerInRange;
  15. float timer;
  16.  
  17.  
  18. void Awake ()
  19. {
  20. player = GameObject.FindGameObjectWithTag ("Player");
  21. playerHealth = player.GetComponent <PlayerHealth> ();
  22. enemyHealth = GetComponent<EnemyHealth>();
  23. anim = GetComponent <Animator> ();
  24. }
  25.  
  26.  
  27. void OnTriggerEnter (Collider other)
  28. {
  29. if(other.gameObject == player)
  30. {
  31. playerInRange = true;
  32. }
  33. }
  34.  
  35.  
  36. void OnTriggerExit (Collider other)
  37. {
  38. if(other.gameObject == player)
  39. {
  40. playerInRange = false;
  41. }
  42. }
  43.  
  44.  
  45. void Update ()
  46. {
  47. timer += Time.deltaTime;
  48.  
  49. if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
  50. {
  51. Attack ();
  52. }
  53.  
  54. if(playerHealth.currentHealth <= 0)
  55. {
  56. anim.SetTrigger ("PlayerDead");
  57. }
  58. }
  59.  
  60.  
  61. void Attack ()
  62. {
  63. timer = 0f;
  64.  
  65. if(playerHealth.currentHealth > 0)
  66. {
  67. playerHealth.TakeDamage (attackDamage);
  68. }
  69. }
  70. }
Add Comment
Please, Sign In to add comment