Advertisement
Guest User

Speedway

a guest
Oct 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7. public GameObject turningPoint;
  8. public GameObject accidentTarget;
  9. public GameObject turningSpeedSetter;
  10. public float turningPointMoveSpeedUp;
  11. public float turningPointMoveSpeedDown;
  12. public float turningPointMoveSpeedRight;
  13. public float turningPointMoveSpeedLeft;
  14. public float moveSpeed = 10f;
  15. public float turningSpeed = 150f;
  16. public float rotatingSpeed = 150f;
  17. public float accidentSpeed;
  18. public bool turning = false;
  19. public bool driving = true;
  20. public bool moveTurningTargetUp = false;
  21. public bool moveTurningTargetDown = false;
  22. public bool turningLeft;
  23. public bool accident;
  24. public bool turningMode;
  25. public bool enemyAccident;
  26. public int number;
  27.  
  28. Rigidbody2D rb;
  29. Animator animator;
  30.  
  31. Vector3 currentRotation;
  32. Vector2 turningPointPos;
  33. Vector3 speedSetterDis;
  34.  
  35. bool canSetTurningPoint = false;
  36. float disY;
  37.  
  38. void Start()
  39. {
  40. animator = GetComponent<Animator>();
  41. rb = GetComponent<Rigidbody2D>();
  42. }
  43.  
  44. void Update()
  45. {
  46. turningPointPos = turningPoint.transform.localPosition;
  47.  
  48. turningPointPos.y = Mathf.Clamp(turningPointPos.y, -1.7f, 1.7f);
  49. turningPointPos.x = Mathf.Clamp(turningPointPos.x, -4.54f, 4.54f);
  50.  
  51. turningPoint.transform.localPosition = turningPointPos;
  52.  
  53. if (enemyAccident == true)
  54. {
  55. StartCoroutine(EnemyAccident());
  56. }
  57.  
  58. Move();
  59. }
  60.  
  61. private void Move()
  62. {
  63. if (accident == false)
  64. {
  65. if (driving == true)
  66. {
  67. transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
  68. }
  69.  
  70. if (turning == true)
  71. {
  72. TurningTime();
  73. }
  74.  
  75. if (turningMode == true)
  76. {
  77. if (turning == true)
  78. {
  79. driving = false;
  80.  
  81. moveTurningTargetDown = false;
  82. moveTurningTargetUp = true;
  83.  
  84. if (moveTurningTargetUp == true)
  85. {
  86. turningPoint.transform.Translate(Vector2.up * turningPointMoveSpeedUp * Time.deltaTime);
  87. turningPoint.transform.Translate(Vector2.right * turningPointMoveSpeedRight * Time.deltaTime);
  88. }
  89.  
  90. if (turningLeft == true)
  91. {
  92. if (turningMode == true)
  93. {
  94. transform.Rotate(Vector3.forward, Time.deltaTime * rotatingSpeed);
  95. }
  96. }
  97.  
  98. currentRotation = transform.localRotation.eulerAngles;
  99. currentRotation.z = Mathf.Clamp(currentRotation.z, 0, 200);
  100. transform.localRotation = Quaternion.Euler(currentRotation);
  101. }
  102. }
  103.  
  104. if (turningMode == false)
  105. {
  106. if (turning == true)
  107. {
  108. moveTurningTargetDown = true;
  109. }
  110. }
  111.  
  112. if (turning == true)
  113. {
  114. if (moveTurningTargetDown == true)
  115. {
  116. turningPoint.transform.Translate(Vector2.down * turningPointMoveSpeedDown * Time.deltaTime);
  117. turningPoint.transform.Translate(Vector2.left * turningPointMoveSpeedLeft * Time.deltaTime);
  118. }
  119.  
  120. transform.RotateAround(turningPoint.transform.position, new Vector3(0, 0, 1), turningSpeed * Time.deltaTime);
  121. }
  122.  
  123. SetTurningPoint();
  124. }
  125. }
  126.  
  127. public void TurningTime()
  128. {
  129. StartCoroutine(Turning());
  130. }
  131.  
  132. IEnumerator Turning()
  133. {
  134. disY = speedSetterDis.y;
  135.  
  136. if (number == 0)
  137. {
  138. disY = disY * -1;
  139. print(disY);
  140. }
  141.  
  142. if (disY <= 0.36f)
  143. {
  144. yield return new WaitForSeconds(0.01f);
  145. turningMode = true;
  146. yield return new WaitForSeconds(0.15f);
  147. turningMode = false;
  148. }
  149. if (disY <= 1.08f || disY <= 1.6f)
  150. {
  151. yield return new WaitForSeconds(0.2f);
  152. turningMode = true;
  153. yield return new WaitForSeconds(0.1f);
  154. turningMode = false;
  155. }
  156. }
  157.  
  158. private void SetTurningPoint()
  159. {
  160. if (canSetTurningPoint == true)
  161. {
  162. if (number == 1)
  163. {
  164. turningPoint.transform.position = new Vector3(4.4f, 1.53f, 0);
  165. }
  166. else if (number == 0)
  167. {
  168. turningPoint.transform.position = new Vector3(-4.4f, -1.53f, 0);
  169. }
  170. }
  171. canSetTurningPoint = false;
  172. }
  173.  
  174. IEnumerator EnemyAccident()
  175. {
  176. Accident();
  177. yield return new WaitForSeconds(0.2f);
  178. enemyAccident = false;
  179. }
  180.  
  181. public void Accident()
  182. {
  183. accident = true;
  184. animator.SetBool("Accident", true);
  185. Vector2 target = accidentTarget.transform.position;
  186. transform.position = Vector2.MoveTowards(transform.position, target, accidentSpeed * Time.deltaTime);
  187. }
  188.  
  189. private void OnTriggerEnter2D(Collider2D collision)
  190. {
  191. turningPointMoveSpeedUp = Random.Range(2, 3);
  192. turningPointMoveSpeedDown = Random.Range(4, 5);
  193. turningPointMoveSpeedRight = Random.Range(2, 3);
  194. turningPointMoveSpeedLeft = Random.Range(3, 5);
  195.  
  196. if (collision.gameObject.tag == "TurningColliderON")
  197. {
  198. driving = false;
  199. turning = true;
  200. moveTurningTargetDown = true;
  201. turningLeft = true;
  202. transform.localRotation = Quaternion.Euler(0, 0, 0);
  203. }
  204. else if (collision.gameObject.tag == "TurningColliderOFF")
  205. {
  206. driving = true;
  207. turning = false;
  208. turningLeft = false;
  209. if (number == 0)
  210. {
  211. transform.localRotation = Quaternion.Euler(0, 0, 0);
  212. }
  213. else if (number == 1)
  214. {
  215. transform.localRotation = Quaternion.Euler(0, 0, 180);
  216. }
  217. }
  218. else if (collision.gameObject.tag == "RightTurn")
  219. {
  220. speedSetterDis = turningSpeedSetter.transform.position - transform.position;
  221. StartCoroutine(ChangeSpeedSetterPositionToUp());
  222. canSetTurningPoint = true;
  223. turningPoint.transform.localRotation = Quaternion.Euler(0, 0, 0);
  224. number = 1;
  225. }
  226. else if (collision.gameObject.tag == "LeftTurn")
  227. {
  228. speedSetterDis = turningSpeedSetter.transform.position - transform.position;
  229. StartCoroutine(ChangeSpeedSetterPositionToDown());
  230. canSetTurningPoint = true;
  231. turningPoint.transform.localRotation = Quaternion.Euler(0, 0, 180);
  232. number = 0;
  233. }
  234. else if (collision.gameObject.tag == "LoseCollider")
  235. {
  236. enemyAccident = true;
  237. }
  238. }
  239.  
  240. IEnumerator ChangeSpeedSetterPositionToUp()
  241. {
  242. yield return new WaitForSeconds(2);
  243. turningSpeedSetter.transform.position = new Vector3(-2.5f, 2f, 0);
  244. }
  245.  
  246. IEnumerator ChangeSpeedSetterPositionToDown()
  247. {
  248. yield return new WaitForSeconds(2);
  249. turningSpeedSetter.transform.position = new Vector3(2f, -2f, 0);
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement