Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using UnityEngine.Events;
  6.  
  7. public class Enemy : MonoBehaviour {
  8.  
  9. private GameObject target;
  10.  
  11. private NavMeshAgent agent;
  12.  
  13. private Health health;
  14.  
  15. private Animator animator;
  16.  
  17. private Collider collider;
  18.  
  19. private Health targetHealth;
  20.  
  21. private Player player;
  22.  
  23. [HideInInspector] public bool isAttacking = false;
  24.  
  25. [HideInInspector] public bool isDead = false;
  26.  
  27. public float speed = 1.0f;
  28.  
  29. public float angularSpeed = 120;
  30.  
  31. public float damage = 20;
  32.  
  33. public float attackAngle = 45;
  34.  
  35. [HideInInspector] public UnityEvent onDead;
  36.  
  37. void Start() {
  38. target = GameObject.Find("Player");
  39. targetHealth = target.GetComponent<Health>();
  40.  
  41. if (target == null){
  42. throw new System.Exception("Target dosen't have Health Component.");
  43. }
  44.  
  45. player = target.GetComponent<Player>();
  46.  
  47. if (player == null)
  48. {
  49. throw new System.Exception("Player dosen't have Player Component.");
  50. }
  51.  
  52. agent = GetComponent<NavMeshAgent>();
  53. health = GetComponent<Health>();
  54. animator = GetComponent<Animator>();
  55. collider = GetComponent<Collider>();
  56. }
  57.  
  58. void Update() {
  59. CheckHealth();
  60. Chase();
  61. CheckAttack();
  62. }
  63.  
  64. void CheckHealth() {
  65. if (isDead) return;
  66.  
  67. if (health.value <= 0) {
  68. onDead.Invoke();
  69.  
  70. isDead = true;
  71. agent.isStopped = true;
  72. collider.enabled = false;
  73.  
  74. animator.CrossFadeInFixedTime("Death", 0.1f);
  75. Destroy(gameObject, 3f);
  76. }
  77. }
  78.  
  79. void Chase() {
  80. if (isDead) return;
  81. else if (player.isDead) return;
  82.  
  83. agent.destination = target.transform.position;
  84. }
  85.  
  86. void CheckAttack() {
  87. if (isDead) return;
  88. else if (isAttacking) return;
  89. else if (player.isDead) return;
  90.  
  91. float distanceFromTarget = Vector3.Distance(target.transform.position, transform.position);
  92.  
  93. if (distanceFromTarget <= 1.8f) {
  94. Vector3 directionToTarget = target.transform.position - transform.position;
  95. float angle = Vector3.Angle(directionToTarget, transform.forward);
  96.  
  97. if(angle <= attackAngle) {
  98. Attack();
  99. }
  100. }
  101. }
  102.  
  103. void Attack() {
  104. targetHealth.TakeDamage(damage);
  105.  
  106. agent.speed = 0;
  107. agent.angularSpeed = 0;
  108. isAttacking = true;
  109. animator.SetTrigger("ShouldAttack");
  110.  
  111. Invoke("ResetAttacking", 1.5f);
  112. }
  113.  
  114. void ResetAttacking() {
  115. isAttacking = false;
  116. agent.speed = speed;
  117. agent.angularSpeed = angularSpeed;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement