Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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. if (Vector3.Distance(transform.position, TargetPosition) <= GetComponent<Statistics>().AttackRange)
  37. {
  38. DoDamage();
  39. }
  40.  
  41. //jesli zombie osiagnac swoj cel, wylosuj kolejny punkt o ktorego ma sie udac
  42. if (Vector3.Distance(transform.position, TargetPosition) <= Radius)
  43. {
  44. TargetPosition = Waypoint.GetRandomWaypoint();
  45. }
  46. Movement();
  47. }
  48.  
  49. void Movement()
  50. {
  51. var direction = (Vector3)TargetPosition - transform.position;
  52. var zombieVelocity = direction.normalized * Speed;
  53.  
  54. Rigidbody.MovePosition(Rigidbody.position + (Vector2)zombieVelocity * Time.fixedDeltaTime);
  55. transform.right = (Vector2)direction;
  56. }
  57.  
  58. void DoDamage()
  59. {
  60. Player.GetComponent<Statistics>().Health -= GetComponent<Statistics>().AttackDamage;
  61. }
  62.  
  63. private void OnTriggerEnter2D(Collider2D collision)
  64. {
  65. RunningToPlayer = true;
  66. Speed = 3f;
  67. }
  68. private void OnTriggerExit2D(Collider2D collision)
  69. {
  70. RunningToPlayer = false;
  71. Speed = 1f;
  72. TargetPosition = transform.position;
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement