Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. public class Zombie : MonoBehaviour
  2. {
  3.  
  4. Vector2 TargetPosition;
  5. Rigidbody2D Rigidbody;
  6. Player Player;
  7. Waypoint Waypoint;
  8.  
  9. [SerializeField]
  10. float Speed = 5f;
  11.  
  12. [SerializeField]
  13. float Radius = 0.5f;
  14.  
  15.  
  16. public float TimeBetweenAttack = 1;
  17.  
  18. bool RunningToPlayer = false;
  19.  
  20.  
  21. void Start ()
  22. {
  23.  
  24. Player = FindObjectOfType<Player>();
  25. TargetPosition = transform.position;
  26. Waypoint = FindObjectOfType<Waypoint>();
  27. Rigidbody = GetComponent<Rigidbody2D>();
  28. TargetPosition = Waypoint.GetRandomWaypoint();
  29. StartCoroutine(CheckIfZombieCanAttackCoroutine());
  30. }
  31.  
  32.  
  33. void FixedUpdate ()
  34. {
  35. //jeśli gracz wejdzie w pole widzenia zombie to wtedy zombie zmierza do pozycji gracza
  36.  
  37. if (RunningToPlayer == true)
  38. {
  39. Movement();
  40. TargetPosition = Player.transform.position;
  41.  
  42.  
  43. }
  44. //jesli zombie osiagnac swoj cel, wylosuj kolejny punkt o ktorego ma sie udac
  45. if (RunningToPlayer == false)
  46. {
  47. Movement();
  48. if (Vector3.Distance(transform.position, TargetPosition) <= Radius)
  49. TargetPosition = Waypoint.GetRandomWaypoint();
  50. }
  51.  
  52. }
  53.  
  54. IEnumerator CheckIfZombieCanAttackCoroutine()
  55. {
  56. while (true)
  57. {
  58. if (Vector3.Distance(transform.position, Player.transform.position) <= GetComponent<Statistics>().AttackRange)
  59. {
  60. DoDamage();
  61. }
  62. yield return new WaitForSeconds(TimeBetweenAttack);
  63. }
  64. }
  65. void Movement()
  66. {
  67. var direction = (Vector3)TargetPosition - transform.position;
  68. var zombieVelocity = direction.normalized * Speed;
  69.  
  70. Rigidbody.MovePosition(Rigidbody.position + (Vector2)zombieVelocity * Time.fixedDeltaTime);
  71. transform.right = (Vector2)direction;
  72. }
  73.  
  74. void DoDamage()
  75. {
  76.  
  77. if (Player.GetComponent<Statistics>().Health > 0)
  78. Player.GetComponent<Statistics>().Health -= GetComponent<Statistics>().AttackDamage;
  79. else return;
  80. }
  81.  
  82.  
  83. private void OnTriggerEnter2D(Collider2D collision)
  84. {
  85. RunningToPlayer = true;
  86. Speed = 3f;
  87. }
  88. private void OnTriggerExit2D(Collider2D collision)
  89. {
  90. RunningToPlayer = false;
  91.  
  92. Speed = 1f;
  93. TargetPosition = transform.position;
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement