EnemyAttack.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using System.Collections;
  3. public class EnemyAttack : MonoBehaviour
  4. {
  5. public float timeBetweenAttacks = 0.5f;
  6. public int attackDamage = 10;
  7. Animator anim;
  8. GameObject player;
  9. PlayerHealth playerHealth;
  10. EnemyHealth enemyHealth;
  11. bool playerInRange;
  12. float timer;
  13. public bool isboss;
  14. float bosstimer;
  15. public GameObject[] buttle;
  16. void Awake()
  17. {
  18. player = GameObject.FindGameObjectWithTag("Player");
  19. playerHealth = player.GetComponent<PlayerHealth>();
  20. anim = GetComponent<Animator>();
  21. enemyHealth = GetComponent<EnemyHealth>();
  22. }
  23. void FixedUpdate()
  24. {
  25. timer += Time.deltaTime;
  26. bosstimer += Time.deltaTime;
  27. if(timer>= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth>0)
  28. {
  29. Attack();
  30. }
  31. if(playerHealth.currentHealth <= 0)
  32. {
  33. anim.SetTrigger("PlayerDead");
  34. }
  35. if(isboss && bosstimer > 5 && Random.Range(0, 100) > 97)
  36. {
  37. BossAttack();
  38. }
  39. }
  40. void Attack()
  41. {
  42. timer = 0f;
  43. if(playerHealth.currentHealth > 0)
  44. {
  45. playerHealth.TakeDamage(attackDamage);
  46. }
  47. }
  48. void BossAttack()
  49. {
  50. bosstimer = 0;
  51. Instantiate(buttle[Random.Range(0,buttle.Length)], transform.position + transform.forward * 2+new Vector3(0,0.9f,0), transform.rotation);
  52. }
  53. void OnTriggerEnter(Collider col)
  54. {
  55. if(col.gameObject == player)
  56. {
  57. playerInRange = true;
  58. }
  59. }
  60. void OnTriggerExit(Collider col)
  61. {
  62. if (col.gameObject == player)
  63. {
  64. playerInRange = false;
  65. }
  66. }
  67. }