Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 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. bool RunningToPlayer = false;
  17.  
  18. void Start ()
  19. {
  20. Player = FindObjectOfType<Player>();
  21. TargetPosition = transform.position;
  22. Waypoint = FindObjectOfType<Waypoint>();
  23. Rigidbody = GetComponent<Rigidbody2D>();
  24. }
  25.  
  26.  
  27. void FixedUpdate ()
  28. {
  29. //jeśli gracz wejdzie w pole widzenia zombie to wtedy zombie zmierza do pozycji gracza
  30. if(RunningToPlayer == true)
  31. {
  32. TargetPosition = Player.transform.position;
  33. }
  34.  
  35. // jesli gracz znajduje sie w zasiegu ataku - zadaj obrazenia
  36.  
  37.  
  38.  
  39. //jesli zombie osiagnac swoj cel, wylosuj kolejny punkt o ktorego ma sie udac
  40. if (Vector3.Distance(transform.position, TargetPosition) <= Radius)
  41. {
  42. TargetPosition = Waypoint.GetRandomWaypoint();
  43. }
  44. Movement();
  45. DoDamage();
  46.  
  47.  
  48. }
  49.  
  50. void Movement()
  51. {
  52. var direction = (Vector3)TargetPosition - transform.position;
  53. var zombieVelocity = direction.normalized * Speed;
  54.  
  55. Rigidbody.MovePosition(Rigidbody.position + (Vector2)zombieVelocity * Time.fixedDeltaTime);
  56. transform.right = (Vector2)direction;
  57. }
  58.  
  59. void DoDamage()
  60. {
  61. if (Vector3.Distance(transform.position, TargetPosition) <= GetComponent<Statistics>().AttackRange)
  62. Player.GetComponent<Statistics>().Health -= GetComponent<Statistics>().AttackDamage;
  63. else return;
  64. }
  65.  
  66. private void OnTriggerEnter2D(Collider2D collision)
  67. {
  68. RunningToPlayer = true;
  69. Speed = 3f;
  70. }
  71. private void OnTriggerExit2D(Collider2D collision)
  72. {
  73. RunningToPlayer = false;
  74. Speed = 1f;
  75. TargetPosition = transform.position;
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement