using UnityEngine; public class PlayerAttack : MonoBehaviour { [SerializeField] private float attackCooldown; public Transform attackPoint; public float attackRange = 1f; public LayerMask enemyLayers; public int attackDamage = 1; private Animator anim; private float cooldownTimer = Mathf.Infinity; private AudioManager audioManager; private void Awake() { anim = GetComponent(); audioManager = GameObject.FindGameObjectWithTag("Audio")?.GetComponent(); // Assign AudioManager here } private void Update() { if (Input.GetMouseButton(0) && cooldownTimer > attackCooldown) Attack(); cooldownTimer += Time.deltaTime; } private void Attack() { if (audioManager != null) { audioManager.PlaySFX(audioManager.swordhit); } anim.SetTrigger("attack"); cooldownTimer = 0; // Detect enemies Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); // Damage enemies foreach (Collider2D enemy in hitEnemies) { Health enemyHealth = enemy.GetComponent(); if (enemyHealth != null) // Check if Health component exists { enemyHealth.TakeDamage(attackDamage); } } } void OnDrawGizmosSelected() { if (attackPoint == null) return; Gizmos.DrawWireSphere(attackPoint.position, attackRange); } }