Advertisement
MatiGe

Soldier

Dec 13th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | Gaming | 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.  
  79.  
  80. timer += Time.deltaTime;
  81. }
  82.  
  83. private void FixedUpdate()
  84. {
  85. if (enemySighted)
  86. {
  87. Vector3 temp = Vector3.MoveTowards(transform.position, bj.position, speed * Time.deltaTime);
  88. FollowPlayer(temp += transform.position);
  89. }
  90. }
  91.  
  92. void Patrol()
  93. {
  94. rb.velocity = new Vector2(dir.x * speed, dir.y * speed);
  95. transform.position = Vector3.MoveTowards(transform.position, wp[wpI].transform.position, speed * Time.deltaTime);
  96.  
  97. if(Vector3.Distance(transform.position, wp[wpI].transform.position)<dist)
  98. {
  99. wpI++;
  100. if(wpI>=wp.Count)
  101. {
  102. wpI = 0;
  103. }
  104. }
  105. }
  106.  
  107. void Dead()
  108. {
  109. rb.velocity = Vector2.zero;
  110. /*rb.constraints = RigidbodyConstraints2D.FreezePositionX;
  111. rb.constraints = RigidbodyConstraints2D.FreezePositionY;
  112. rb.constraints = RigidbodyConstraints2D.FreezeRotation;*/
  113. patrol = false;
  114. transform.gameObject.tag = "DeadSoldier";
  115. }
  116.  
  117. private void Shoot()
  118. {
  119. anim.SetTrigger("Attack");
  120. timer = 0;
  121.  
  122. var bullet = Instantiate(bullets, firepoint.position, Quaternion.identity).GetComponent<Bullet>();
  123.  
  124. //Instantiate(bullets, firepoint.position, Quaternion.identity);
  125.  
  126. bullet.Dir = this.mov;
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. }
  134.  
  135. public void SetAnimFloat(Vector2 setVector)
  136. {
  137. anim.SetFloat("X", setVector.x);
  138. anim.SetFloat("Y", setVector.y);
  139. }
  140.  
  141. public void FollowPlayer(Vector2 direction)
  142. {
  143. //bj = GameObject.FindWithTag("BJ").transform;
  144.  
  145. if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
  146. {
  147. if (direction.x > 0)
  148. {
  149. SetAnimFloat(Vector2.right);
  150. }
  151. else if (direction.x < 0)
  152. {
  153. SetAnimFloat(Vector2.left);
  154. }
  155. }
  156. else if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y))
  157. {
  158. if (direction.y > 0)
  159. {
  160. SetAnimFloat(Vector2.up);
  161. }
  162. else if (direction.y < 0)
  163. {
  164. SetAnimFloat(Vector2.down);
  165. }
  166. }
  167.  
  168.  
  169. }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement