Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Zombie : MonoBehaviour
- {
- Vector2 TargetPosition;
- Rigidbody2D Rigidbody;
- Player Player;
- Waypoint Waypoint;
- [SerializeField]
- float Speed = 5f;
- [SerializeField]
- float Radius = 0.5f;
- bool RunningToPlayer = false;
- void Start ()
- {
- Player = FindObjectOfType<Player>();
- TargetPosition = transform.position;
- Waypoint = FindObjectOfType<Waypoint>();
- Rigidbody = GetComponent<Rigidbody2D>();
- }
- void FixedUpdate ()
- {
- //jeśli gracz wejdzie w pole widzenia zombie to wtedy zombie zmierza do pozycji gracza
- if(RunningToPlayer == true)
- {
- TargetPosition = Player.transform.position;
- }
- // jesli gracz znajduje sie w zasiegu ataku - zadaj obrazenia
- if (Vector3.Distance(transform.position, TargetPosition) <= GetComponent<Statistics>().AttackRange)
- {
- DoDamage();
- }
- //jesli zombie osiagnac swoj cel, wylosuj kolejny punkt o ktorego ma sie udac
- if (Vector3.Distance(transform.position, TargetPosition) <= Radius)
- {
- TargetPosition = Waypoint.GetRandomWaypoint();
- }
- Movement();
- }
- void Movement()
- {
- var direction = (Vector3)TargetPosition - transform.position;
- var zombieVelocity = direction.normalized * Speed;
- Rigidbody.MovePosition(Rigidbody.position + (Vector2)zombieVelocity * Time.fixedDeltaTime);
- transform.right = (Vector2)direction;
- }
- void DoDamage()
- {
- Player.GetComponent<Statistics>().Health -= GetComponent<Statistics>().AttackDamage;
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- RunningToPlayer = true;
- Speed = 3f;
- }
- private void OnTriggerExit2D(Collider2D collision)
- {
- RunningToPlayer = false;
- Speed = 1f;
- TargetPosition = transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement