document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using UnityEngine;
  2.  
  3. public class Enemy : MonoBehaviour
  4. {
  5.     public float speed = 2f;
  6.     public float maxHealth = 40f;
  7.     public float damage = 10f;
  8.     private float currentHealth;
  9.     private Transform player;
  10.  
  11.     void Start()
  12.     {
  13.         currentHealth = maxHealth;
  14.         player = GameObject.FindGameObjectWithTag("Player").transform;
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         if (player != null)
  20.         {
  21.             // Mover hacia el jugador
  22.             Vector2 direction = (player.position - transform.position).normalized;
  23.             transform.Translate(direction * speed * Time.deltaTime);
  24.         }
  25.     }
  26.  
  27.     public void TakeDamage(float amount)
  28.     {
  29.         currentHealth -= amount;
  30.         if (currentHealth <= 0)
  31.         {
  32.             Destroy(gameObject);
  33.             GameController.instance.IncreaseScore(1);
  34.         }
  35.     }
  36.  
  37.     private void OnCollisionEnter2D(Collision2D collision)
  38.     {
  39.         if (collision.gameObject.CompareTag("Player"))
  40.         {
  41.             // Obtener daƱo del enemigo
  42.             PlayerController playerController = collision.gameObject.GetComponent<PlayerController>();
  43.             if (playerController != null)
  44.             {
  45.                 playerController.currentHealth -= damage;
  46.                 if (playerController.currentHealth <= 0)
  47.                 {
  48.                     GameController.instance.GameOver();
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
  54.  
');