Advertisement
MatiGe

Soldier

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