Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyMovement : MonoBehaviour
  5. {
  6. public float movSpeed, rotSpeed;
  7. public Transform[] waypoints;
  8. public int curWaypoint, nextWaypoint;
  9. Quaternion targetRotation;
  10. public float y;
  11. bool choosing, goingStraight;
  12. public float curveSpeed, curve;
  13. public float attackRange;
  14. public float chaseRange;
  15. Vector3 targetPosition, playerPosition;
  16. PlayerHealth player;
  17.  
  18.  
  19. NavMeshAgent agent;
  20. Animator anim;
  21.  
  22. public enum EnemyState { idle, patroling, chasing, attacking, escaping, dying };
  23. public EnemyState eState= 0;
  24. public int animVariable = 0;
  25. public float fieldOfView = 90;
  26.  
  27.  
  28.  
  29.  
  30.  
  31. void Start() {
  32. animVariable = 0;
  33. player = FindObjectOfType<PlayerHealth>();
  34. // goingStraight = true;
  35. /* transform.position = waypoints[0].position + Vector3.up * y;
  36. StartCoroutine(Turning());
  37. StartCoroutine(CheckPlayerPosition());*/
  38. agent = GetComponent<NavMeshAgent>();
  39. anim = GetComponent<Animator>();
  40. eState = EnemyState.idle;
  41. }
  42.  
  43.  
  44. void Update()
  45. {
  46. Sight(transform, player.transform,fieldOfView, "Player");
  47. SetAnimVariable();
  48. switch (eState)
  49. {
  50. case EnemyState.idle:
  51. Idling();
  52. break;
  53. case EnemyState.patroling:
  54. Patroling();
  55. break;
  56. case EnemyState.chasing:
  57. Chasing();
  58. break;
  59. case EnemyState.attacking:
  60. Attacking();
  61. break;
  62. case EnemyState.escaping:
  63. break;
  64. case EnemyState.dying:
  65. break;
  66. default:
  67. break;
  68. }
  69.  
  70.  
  71. }
  72.  
  73. void SetAnimVariable() {
  74. switch (eState) {
  75. case EnemyState.idle:
  76. animVariable = 0;
  77. break;
  78. case EnemyState.chasing:
  79. lol();
  80. break;
  81. case EnemyState.attacking:
  82. animVariable = 2;
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88.  
  89. void lol() {
  90. animVariable = 1;
  91. }
  92.  
  93. void Idling()
  94. {
  95. //animVariable = 0;
  96. /* transform.position += transform.forward * movSpeed * Time.deltaTime;
  97. transform.Rotate(0, curve, 0);
  98. //if (goingStraight)
  99. curve = (Mathf.PingPong(Time.time, curveSpeed) - curveSpeed / 2);*/
  100. }
  101.  
  102. void Patroling()
  103. {
  104. targetPosition = waypoints[nextWaypoint].position;
  105. transform.position = Vector3.MoveTowards(transform.position, targetPosition + Vector3.up * y, movSpeed * Time.deltaTime);
  106. targetRotation = Quaternion.LookRotation(targetPosition + Vector3.up * y - transform.position);
  107. transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotSpeed * Time.deltaTime);
  108. if ((targetPosition + Vector3.up * y - transform.position).magnitude < 1f)
  109. {
  110. StartCoroutine(ChooseNextWaypoint());
  111. }
  112. }
  113.  
  114. void Chasing(){
  115. // animVariable = 1;
  116. LookAtPlayer();
  117. if (attackRange > PlayerEnemyDistance) {
  118. eState = EnemyState.attacking;
  119. }
  120. else if(PlayerEnemyDistance > chaseRange) {
  121. eState = EnemyState.idle;
  122. }
  123.  
  124. /* targetPosition = playerPosition;
  125. transform.position = Vector3.MoveTowards(transform.position, targetPosition + Vector3.up * y, movSpeed * Time.deltaTime);
  126. targetRotation = Quaternion.LookRotation(targetPosition + Vector3.up * y - transform.position);
  127. transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotSpeed * Time.deltaTime);
  128. if ((targetPosition + Vector3.up * y - transform.position).magnitude < 1f)
  129. {
  130. StartCoroutine(ChooseNextWaypoint());
  131. }*/
  132. }
  133.  
  134. void Attacking()
  135. {
  136. // animVariable = 2;
  137. agent.Stop();
  138. if (PlayerEnemyDistance > attackRange)
  139. eState = EnemyState.chasing;
  140.  
  141. }
  142.  
  143. IEnumerator ChooseNextWaypoint()
  144. {
  145. if (!choosing)
  146. {
  147. choosing = true;
  148. yield return new WaitForSeconds(1f);
  149. nextWaypoint++;
  150. if (nextWaypoint == waypoints.Length) nextWaypoint = 0;
  151. choosing = false;
  152. StopCoroutine(ChooseNextWaypoint());
  153. }
  154. }
  155.  
  156.  
  157.  
  158. IEnumerator Turning()
  159. {
  160. while (eState == EnemyState.idle)
  161. {
  162. float orCurve = curve;
  163. goingStraight = false;
  164. curve = (Random.Range(0, 6) > 3) ? 3f : -3f;
  165. yield return new WaitForSeconds(Random.Range(.1f, 1f));
  166. curve = orCurve;
  167. yield return new WaitForSeconds(Random.Range(1f, 5f));
  168. goingStraight = true;
  169. }
  170.  
  171. }
  172.  
  173. IEnumerator CheckPlayerPosition()
  174. {
  175. if (eState == EnemyState.chasing)
  176. {
  177. // playerPosition = player.transform.position;
  178. yield return new WaitForSeconds(2f);
  179. }
  180. }
  181.  
  182. void Sight(Transform me, Transform target, float angleRange, string targetTag) {
  183. Vector3 direction = target.position - me.position;
  184. float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
  185. if (IsTargerWithinAngleRange(me, angle, angleRange)) {
  186. Ray ray = new Ray(me.GetChild(0).position, direction);
  187. RaycastHit hit;
  188. if (Physics.Raycast(ray, out hit)) {
  189. if (hit.collider.gameObject.tag == "Player") {
  190. eState = EnemyState.chasing;
  191. }
  192.  
  193. }
  194. }
  195. }
  196.  
  197.  
  198. void LookAtPlayer() {
  199. Vector3 v = player.transform.position - transform.position;
  200. float angle = -Mathf.Rad2Deg * Mathf.Atan2(v.z, v.x);
  201. transform.eulerAngles = new Vector3(0, angle, 0);
  202. }
  203.  
  204. float PlayerEnemyDistance {
  205. get {
  206. return Vector3.Magnitude(player.transform.position - transform.position);
  207. }
  208. }
  209.  
  210. bool IsTargerWithinAngleRange(Transform me, float angle, float angleField) {
  211. return SomeFunctions.Is_C_Between_A_and_B(angle, -me.eulerAngles.y - angleField / 2, -me.eulerAngles.y + angleField / 2);
  212. }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement