Advertisement
MatiGe

Soldier

Dec 13th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Soldier : MonoBehaviour
  6. {
  7. public bool dead;
  8. public Animator anim;
  9. public float speed;
  10. public Rigidbody2D rb;
  11. public Vector3 mov;
  12. public Vector3 dir;
  13. public bool patrol;
  14. public List<Transform> wp;
  15. public int wpI = 0;
  16. public float dist = 0.2f;
  17.  
  18. public float timer = 0.5f;
  19. public float initTimer = 0.5f;
  20. public GameObject bullets;
  21. public Transform firepoint;
  22. public Bullet b;
  23. public Vector2 direction;
  24. public float bSpeed = 10;
  25. public bool enemySighted;
  26. public bool flip;
  27. public Transform bj;
  28. public Vector3 scale;
  29.  
  30. public float dirX;
  31. public float dirY;
  32.  
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. rb = GetComponent<Rigidbody2D>();
  37. anim = GetComponent<Animator>();
  38. transform.position = wp[wpI].transform.position;
  39. }
  40.  
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. dirX = dir.x;
  45. dirY = dir.y;
  46.  
  47. scale = transform.localScale;
  48.  
  49. mov = new Vector3(dirX, dirY).normalized;
  50.  
  51. if (patrol == true)
  52. {
  53. Patrol();
  54.  
  55.  
  56. }
  57.  
  58. dir = wp[wpI].position - transform.position;
  59. float angle = Mathf.Atan2(dirX, dirY) * Mathf.Rad2Deg;
  60. dir.Normalize();
  61. mov = dir;
  62. anim.SetFloat("X", dirX);
  63. anim.SetFloat("Y", dirY);
  64. anim.SetBool("Dead", dead);
  65. firepoint.position = transform.position + mov.normalized;
  66. b.rb.velocity = this.mov;
  67. mov = new Vector2(dirX * bSpeed, dirY * bSpeed);
  68. if (dead == true)
  69. {
  70. Dead();
  71.  
  72. }
  73. if(enemySighted == true && timer > initTimer)
  74. {
  75. Shoot();
  76. }
  77.  
  78. bj = GameObject.FindWithTag("BJ").transform;
  79.  
  80. float angleX = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
  81.  
  82.  
  83. if (angleX < 0)
  84. {
  85. anim.SetFloat("X", dirX);
  86. }
  87. if (angleX > 0)
  88. {
  89. anim.SetFloat("X", -dirX);
  90. }
  91.  
  92. float angleY = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
  93.  
  94. if (angleY < 0)
  95. {
  96. anim.SetFloat("Y", dirY);
  97. }
  98. if (angleY > 0)
  99. {
  100. anim.SetFloat("Y", -dirY);
  101. }
  102.  
  103. timer += Time.deltaTime;
  104. }
  105.  
  106. private void FixedUpdate()
  107. {
  108. if (enemySighted)
  109. {
  110. Vector3 temp = Vector3.MoveTowards(transform.position, bj.position, speed * Time.deltaTime);
  111. FollowPlayer(temp += transform.position);
  112. }
  113.  
  114. bj = GameObject.FindWithTag("BJ").transform;
  115.  
  116. float angleX = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
  117.  
  118.  
  119. if (angleX < 0)
  120. {
  121. anim.SetFloat("X", dirX);
  122. }
  123. if (angleX > 0)
  124. {
  125. anim.SetFloat("X", -dirX);
  126. }
  127.  
  128. float angleY = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
  129.  
  130. if (angleY < 0)
  131. {
  132. anim.SetFloat("Y", dirY);
  133. }
  134. if (angleY > 0)
  135. {
  136. anim.SetFloat("Y", -dirY);
  137. }
  138. }
  139.  
  140. void Patrol()
  141. {
  142. rb.velocity = new Vector2(dir.x * speed, dir.y * speed);
  143. transform.position = Vector3.MoveTowards(transform.position, wp[wpI].transform.position, speed * Time.deltaTime);
  144.  
  145. if(Vector3.Distance(transform.position, wp[wpI].transform.position)<dist)
  146. {
  147. wpI++;
  148. if(wpI>=wp.Count)
  149. {
  150. wpI = 0;
  151. }
  152. }
  153. }
  154.  
  155. void Dead()
  156. {
  157. rb.velocity = Vector2.zero;
  158. /*rb.constraints = RigidbodyConstraints2D.FreezePositionX;
  159. rb.constraints = RigidbodyConstraints2D.FreezePositionY;
  160. rb.constraints = RigidbodyConstraints2D.FreezeRotation;*/
  161. patrol = false;
  162. transform.gameObject.tag = "DeadSoldier";
  163. }
  164.  
  165. private void Shoot()
  166. {
  167. anim.SetTrigger("Attack");
  168. timer = 0;
  169.  
  170. var bullet = Instantiate(bullets, firepoint.position, Quaternion.identity).GetComponent<Bullet>();
  171.  
  172. //Instantiate(bullets, firepoint.position, Quaternion.identity);
  173.  
  174. bullet.Dir = this.mov;
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. }
  182.  
  183. public void SetAnimFloat(Vector2 setVector)
  184. {
  185. anim.SetFloat("X", setVector.x);
  186. anim.SetFloat("Y", setVector.y);
  187. }
  188.  
  189. public void FollowPlayer(Vector2 direction)
  190. {
  191. bj = GameObject.FindWithTag("BJ").transform;
  192.  
  193. float angleX = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
  194.  
  195. if (angleX > 0)
  196. {
  197. anim.SetFloat("X", dirX);
  198. }
  199. if (angleX < 0)
  200. {
  201. anim.SetFloat("X", -dirX);
  202. }
  203.  
  204. if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
  205. {
  206. if (direction.x > 0)
  207. {
  208. SetAnimFloat(Vector2.right);
  209. }
  210. else if (direction.x < 0)
  211. {
  212. SetAnimFloat(Vector2.left);
  213. }
  214. }
  215. else if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y))
  216. {
  217. if (direction.y > 0)
  218. {
  219. SetAnimFloat(Vector2.up);
  220. }
  221. else if (direction.y < 0)
  222. {
  223. SetAnimFloat(Vector2.down);
  224. }
  225. }
  226.  
  227.  
  228. }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement