using UnityEngine; public class PlayerAttack : MonoBehaviour { public Animator animator; public Transform attackPoint; public float attackRange = 0.5f; public int attackDamage = 1; public LayerMask enemyLayers; public float attackCooldown = 1f; // Time between attacks private float lastAttackTime = -Mathf.Infinity; void Update() { if (Input.GetKeyDown(KeyCode.Space) && Time.time >= lastAttackTime + attackCooldown) { Attack(); } } void Attack() { // Record the time of the attack lastAttackTime = Time.time; // Play attack animation animator.SetTrigger("Attack"); // Detect enemies in attack range Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); foreach (Collider2D enemyCollider in hitEnemies) { Enemy enemy = enemyCollider.GetComponent(); if (enemy != null) { enemy.TakeDamage(attackDamage); } } } }