Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Enemy : MonoBehaviour
- {
- private SpriteRenderer sprite;
- public GameObject hitParticle;
- public GameObject hitBox;
- private Vector3 currentTarget;
- public Transform pointA, pointB;
- public int health;
- public float speed;
- bool canBeDamaged = true;
- private void Start()
- {
- sprite = GetComponentInChildren<SpriteRenderer>();
- }
- private void Update()
- {
- Movement();
- }
- public void Movement()
- {
- if (transform.position == pointA.position)
- {
- currentTarget = pointB.position;
- sprite.flipX = false;
- }
- else if (transform.position == pointB.position)
- {
- currentTarget = pointA.position;
- sprite.flipX = true;
- }
- transform.position = Vector3.MoveTowards(transform.localPosition, currentTarget, speed * Time.deltaTime);
- }
- private void OnCollisionEnter2D(Collision2D other)
- {
- if (other.gameObject.CompareTag("Player"))
- {
- if(canBeDamaged)
- {
- Instantiate(hitParticle,transform.position, transform.rotation);
- health--;
- StartCoroutine(CanBeDamagedRoutine());
- canBeDamaged = false;
- }
- if (health < 1)
- {
- Destroy(this.gameObject);
- }
- }
- }
- IEnumerator CanBeDamagedRoutine()
- {
- yield return new WaitForSeconds(0.6f);
- canBeDamaged = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement