You are on page 1of 1

//enemigo controlador

using UnityEngine;

public class EnemyController : MonoBehaviour


{
public float detectionRange = 5f;
public float attackRange = 2f;
public float attackCooldown = 2f;
public float attackDamage = 10f;

private Transform player;


private Rigidbody2D rb;
private bool isAttacking;
private float attackTimer;

void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
}

void Update()
{
// Distancia entre el enemigo y el jugador
float distanceToPlayer = Vector2.Distance(transform.position,
player.position);

// Si el jugador está dentro del rango de detección y fuera


del rango de ataque, persigue al jugador
if (distanceToPlayer <= detectionRange && distanceToPlayer >
attackRange)
{
Vector2 direction = (player.position -
transform.position).normalized;
rb.velocity = direction * 3f; // Velocidad de persecución
}
else
{
rb.velocity = Vector2.zero;
}
}
}

You might also like