Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using System;
  6.  
  7. public class Enemy : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private GameObject bullet;
  11.  
  12. [SerializeField]
  13. private Transform gun;
  14.  
  15. [SerializeField]
  16. private float shootPower = 20000f;
  17.  
  18. [SerializeField]
  19. private float targetEnemyMaxDistance = 50f;
  20.  
  21. [SerializeField]
  22. private float shootDelay = 2.5f;
  23.  
  24. [SerializeField]
  25. private int bulletDamage = 2;
  26.  
  27. [SerializeField]
  28. private Transform[] wayPoints;
  29.  
  30. private Transform target;
  31.  
  32. private NavMeshAgent agent;
  33.  
  34. private Transform player;
  35.  
  36. private bool seeTarget;
  37.  
  38. private Vector3 targetDirection;
  39.  
  40. private int currentWayPoint = 0;
  41.  
  42.  
  43. public Transform Target
  44.  
  45. {
  46. get
  47. {
  48. return target;
  49. }
  50.  
  51. set
  52. {
  53. target = value;
  54. }
  55. }
  56.  
  57. void Awake()
  58. {
  59. agent = GetComponent<NavMeshAgent>();
  60.  
  61. }
  62.  
  63. private void Start()
  64. {
  65. InvokeRepeating("Shoot", 0.0f, shootDelay);
  66. }
  67.  
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. /* if(target!= null)
  72. {
  73. agent.SetDestination(target.position);
  74. CheckTargetVisibility();
  75.  
  76. }
  77. */
  78. GoByWayPoints();
  79. GoToPlayer();
  80.  
  81.  
  82. }
  83.  
  84. private void Shoot()
  85. {
  86. if (seeTarget)
  87. {
  88. GameObject newBullet = Instantiate(bullet, gun.position, gun.rotation);
  89.  
  90. if (targetDirection != null)
  91. {
  92. targetDirection.Normalize();
  93. newBullet.GetComponent<Rigidbody>().AddForce(targetDirection * shootPower);
  94. }
  95.  
  96.  
  97. newBullet.GetComponent<Damager>().Damage = bulletDamage;
  98. Destroy(newBullet, 5);
  99. }
  100. }
  101.  
  102. private void CheckTargetVisibility()
  103. {
  104. RaycastHit hit;
  105. targetDirection = target.position - gun.position;
  106. Ray ray = new Ray(gun.position, targetDirection);
  107.  
  108. if (Physics.Raycast(ray, out hit) && hit.transform == target)
  109. {
  110. seeTarget = true;
  111. return;
  112. }
  113.  
  114.  
  115. seeTarget = false;
  116. }
  117.  
  118. private void GoByWayPoints()
  119. {
  120.  
  121. float minDistance = 50f;
  122.  
  123. if (wayPoints.Length > 0 )
  124. {
  125. try
  126. {
  127. player = GameObject.FindGameObjectWithTag("Player").transform;
  128. }
  129. catch(NullReferenceException e)
  130. {
  131.  
  132. return;
  133. }
  134.  
  135. if (player != null )
  136. {
  137. if((player.position - transform.position).magnitude < targetEnemyMaxDistance)
  138. {
  139. target = player;
  140. agent.SetDestination(target.position);
  141. CheckTargetVisibility();
  142. Debug.Log("GoToPlayer()");
  143. target = null;
  144.  
  145. }
  146.  
  147. else
  148. {
  149. if (target == null || (transform.position - target.position).magnitude <= minDistance)
  150. {
  151. seeTarget = false;
  152. target = wayPoints[currentWayPoint];
  153. agent.SetDestination(target.position);
  154. currentWayPoint++;
  155. Debug.Log(currentWayPoint);
  156. if (currentWayPoint >= wayPoints.Length)
  157. currentWayPoint = 0;
  158. Debug.Log("GoWayPoints()");
  159. }
  160.  
  161. }
  162.  
  163. }
  164. }
  165. }
  166.  
  167. private void GoToPlayer()
  168. {
  169. try
  170. {
  171. player = GameObject.FindGameObjectWithTag("Player").transform;
  172. }
  173. catch (NullReferenceException e)
  174. {
  175. Debug.Log("null");
  176. return;
  177. }
  178. if (target != null && target == player)
  179. {
  180. agent.SetDestination(target.position);
  181. CheckTargetVisibility();
  182. Debug.Log("GoToPlayer()");
  183. }
  184.  
  185.  
  186. }
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement