Advertisement
Guest User

Movement

a guest
Apr 24th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Pathfinding;
  5.  
  6. public class BoxEnemy : MonoBehaviour
  7. {
  8. private GameObject playerP;
  9. private GameObject player;
  10.  
  11. [SerializeField] private Transform target;
  12.  
  13. [SerializeField] private GameObject proj;
  14. [SerializeField] private Transform FirePoint;
  15. [SerializeField] private ParticleSystem p_damage;
  16. [SerializeField] private ParticleSystem p_die;
  17. [SerializeField] private SpriteRenderer sp;
  18. [SerializeField] private float speed = 200f;
  19. [SerializeField] private float jumpSpeed = 200f;
  20. [SerializeField] private float jumpCooldown;
  21. [SerializeField] private float counterMovement;
  22. [SerializeField] private float nextWaypointDistance = 3f;
  23. [SerializeField] private float health = 10;
  24. [SerializeField] private Color damageColor;
  25. [SerializeField] private float fireRate;
  26. [SerializeField] private bool IsGrounded;
  27. private float fireTime;
  28.  
  29. Path path;
  30. int cw = 0;
  31. bool reachedEndOfPath = false;
  32.  
  33. Seeker seeker;
  34. Rigidbody2D rb;
  35. Color oc;
  36.  
  37. void Awake()
  38. {
  39. playerP = GameObject.Find("PlayerParent");
  40. player = playerP.transform.Find("Player").gameObject;
  41.  
  42. seeker = GetComponent<Seeker>();
  43. rb = GetComponent<Rigidbody2D>();
  44.  
  45. oc = sp.color;
  46.  
  47. InvokeRepeating("UpdatePath", 0f, .1f);
  48. }
  49.  
  50. void UpdatePath()
  51. {
  52. if(seeker.IsDone())
  53. seeker.StartPath(rb.position, target.position, OnPathComplete);
  54. }
  55.  
  56. void Shoot()
  57. {
  58. if(fireTime <= 0)
  59. {
  60. Vector3 mousePos2;
  61.  
  62. mousePos2 = player.transform.position;
  63. mousePos2.z = 5.23f;
  64.  
  65. Vector3 objectPos2 = transform.position;
  66. mousePos2.x = mousePos2.x - objectPos2.x;
  67. mousePos2.y = mousePos2.y - objectPos2.y;
  68.  
  69. float a = Mathf.Atan2(mousePos2.y, mousePos2.x) * Mathf.Rad2Deg;
  70. Quaternion dr2 = Quaternion.Euler(new Vector3(0, 0, a));
  71.  
  72. fireTime = fireRate;
  73. Instantiate(proj, FirePoint.position, dr2);
  74. }
  75. }
  76.  
  77. void OnPathComplete(Path p)
  78. {
  79. if(!p.error)
  80. {
  81. path = p;
  82. cw = 0;
  83. }
  84. }
  85. bool died = false;
  86. [SerializeField] Vector2 force;
  87. void Update()
  88. {
  89.  
  90. if (IsGrounded)
  91. {
  92. rb.drag = counterMovement;
  93. }
  94. else
  95. {
  96. rb.drag = 0;
  97. }
  98.  
  99. if(fireTime > 0)
  100. {
  101. fireTime -= Time.deltaTime;
  102. }
  103.  
  104. if(health <= 0 && died == false)
  105. {
  106. died = true;
  107. StartCoroutine(Die());
  108. }
  109.  
  110. if(died)
  111. return;
  112.  
  113. if(path == null)
  114. return;
  115.  
  116. if(cw >= path.vectorPath.Count || path.vectorPath.Count < 1)
  117. {
  118. reachedEndOfPath = true;
  119. return;
  120. } else
  121. {
  122. reachedEndOfPath = false;
  123. }
  124.  
  125. float distance = Vector3.Distance(transform.position, player.transform.position);
  126.  
  127. if(distance <= 2)
  128. {
  129. force = Vector2.zero;
  130. Shoot();
  131. return;
  132. }
  133.  
  134. if(distance <= 5)
  135. {
  136. Shoot();
  137. }
  138.  
  139. Vector2 dir = ((Vector2)path.vectorPath[cw] - rb.position).normalized;
  140. force = dir * speed * Time.deltaTime;
  141.  
  142. float dist = Vector2.Distance(rb.position, path.vectorPath[cw]);
  143.  
  144. if(dist < nextWaypointDistance)
  145. {
  146. cw++;
  147. }
  148. }
  149. bool jumpCoolDown;
  150. private void FixedUpdate()
  151. {
  152. if(force.x >= 0.01f && IsGrounded)
  153. {
  154. rb.AddForce(new Vector2(1 * speed, 0));
  155. }
  156. else if(force.x <= -0.01f && IsGrounded)
  157. {
  158. rb.AddForce(new Vector2(-1 * speed, 0));
  159. }
  160. else if(force.y >= 0.03f && IsGrounded)
  161. {
  162. Jump();
  163. }
  164.  
  165. if(force.x >= 0.01f && !IsGrounded)
  166. {
  167. rb.velocity = new Vector2(1 * speed / 2, rb.velocity.y);
  168. }
  169. else if(force.x <= -0.01f && !IsGrounded)
  170. {
  171. rb.velocity = new Vector2(-1 * speed / 2, rb.velocity.y);
  172. }
  173. }
  174.  
  175. private void Jump()
  176. {
  177. // Makes it so the player cannot spam jumping.
  178. jumpCoolDown = true;
  179. StartCoroutine(JumpReset());
  180.  
  181. // Adds force upward.
  182. rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
  183. }
  184.  
  185. public IEnumerator JumpReset()
  186. {
  187. // Wait for time: 0.2 seconds.
  188. yield return new WaitForSeconds(jumpCooldown);
  189.  
  190. // Sets the jumpCoolDown var to true.
  191. jumpCoolDown = false;
  192. }
  193.  
  194. private void OnCollisionEnter2D(Collision2D other)
  195. {
  196. if(other.transform.tag == "Bullet")
  197. {
  198. Damage(1);
  199. }
  200. if(other.transform.tag == "Ground")
  201. {
  202. IsGrounded = true;
  203. }
  204. }
  205.  
  206. private void OnCollisionExit2D(Collision2D other)
  207. {
  208. if(other.transform.tag == "Ground")
  209. {
  210. IsGrounded = false;
  211. }
  212. }
  213.  
  214. void Damage(float amount)
  215. {
  216. p_damage.Play();
  217. health -= amount;
  218.  
  219. StartCoroutine(DamageIE());
  220. }
  221.  
  222. IEnumerator DamageIE()
  223. {
  224. sp.color = damageColor;
  225.  
  226. yield return new WaitForSeconds(0.1f);
  227.  
  228. sp.color = oc;
  229. }
  230.  
  231. IEnumerator Die()
  232. {
  233. CancelInvoke();
  234. rb.simulated = false;
  235. GetComponent<BoxCollider2D>().enabled = false;
  236. sp.enabled = false;
  237. p_die.Play();
  238.  
  239. yield return new WaitForSeconds(3);
  240.  
  241. Destroy(gameObject);
  242. }
  243. }
  244.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement