2d sword hit register,trying to make a sword 2d

I’m making a sword that moves to the mouse around the player in my 2d top-down game but sometimes the hits arent registering(it happens inconsistently) can anyone help me fix it?

using UnityEngine;

public class SwordController : MonoBehaviour
{
    public float swordDistance = 1.5f;  // distance from object to sword
    public float swordMinDistance = 0.5f;  // minimum distance from object to sword
    public float swordMaxDistance = 2.5f;  // maximum distance from object to sword
    public float swordSpeed = 5.0f;  // speed at which sword moves
    public LayerMask enemyLayer;  // layer mask for enemy objects
    public float swordDamage = 10.0f;  // damage dealt by sword to enemies
    public float swordDelay = 0.2f; // delay between sword swings
    public bool canDamageMultipleEnemies = false; // allow sword to damage multiple enemies in one swing

public Transform targetObject; // the object around which the sword should rotate

public GameObject slashPrefab; // reference to the prefab for the slash animation

private Vector2 swordPosition;  // position of sword
private Vector2 mousePosition;  // position of mouse pointer
private Vector2 targetPosition;  // position of target object
private bool canSwingSword = true; // flag to check if sword can be swung

private void Start()
{
    // initialize sword position to be at default distance from target object
    swordPosition = targetObject.position;
    swordPosition.x = targetObject.position.x + swordDistance;

    // get target object position
    targetPosition = targetObject.position;
}

private bool hasHitEnemy = false; // flag to check if sword has hit an enemy

private void FixedUpdate()
{





    mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    // calculate vector from target object to mouse
    Vector2 direction = mousePosition - targetPosition;

    // limit distance of sword to target object
    swordPosition = targetPosition + Vector2.ClampMagnitude(direction, swordMaxDistance);

    // move sword towards mouse position
    transform.position = Vector2.Lerp(transform.position, swordPosition, swordSpeed * Time.fixedDeltaTime);

    // detect enemies in range of sword
    if (canSwingSword && Input.GetMouseButtonDown(0))
    {
        RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, transform.right, swordDistance, enemyLayer);

        // iterate through all hits
        for (int i = 0; i < hits.Length; i++)
        {
            RaycastHit2D hit = hits*;*

// deal damage to enemy hit by sword
if (hit.collider != null)
{
if (!hasHitEnemy && (canDamageMultipleEnemies || !canDamageMultipleEnemies && !hasHitEnemy))
{
EnemyController enemyController = hit.collider.GetComponent();
DispressionControl dispressionControl = hit.collider.GetComponent();

// check if the enemy collides with the sword range gizmo for damage
if (dispressionControl != null && dispressionControl.canTakeDamage)
{
dispressionControl.TakeDamage(swordDamage);

// spawn slash animation prefab at enemy’s position
GameObject slash = Instantiate(slashPrefab, dispressionControl.transform.position, Quaternion.identity);
Destroy(slash, 0.42f);
}
else if (enemyController != null && enemyController.canTakeDamage)
{
enemyController.TakeDamage(swordDamage);

// spawn slash animation prefab at enemy’s position
GameObject slash = Instantiate(slashPrefab, enemyController.transform.position, Quaternion.identity);
Destroy(slash, 0.42f);
}

hasHitEnemy = true;
}
}
}

// set canSwingSword flag to false to prevent multiple swings in quick succession
canSwingSword = false;
Invoke(“ResetSwordSwing”, swordDelay);
}

if(Input.GetMouseButtonDown(0)){
// Damage only the first enemy in the trigger area
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, swordDistance, enemyLayer);

foreach (Collider2D col in colliders)
{
EnemyController enemyController = col.GetComponent();
DispressionControl dispressionControl = col.GetComponent();

if (enemyController != null && enemyController.canTakeDamage)
{
enemyController.TakeDamage(swordDamage);

// spawn the slash animation object
GameObject slashObject = Instantiate(slashPrefab, col.transform.position, Quaternion.identity);
Destroy(slashObject, 0.42f); // destroy the animation object after 1 second

break;
}
else if (dispressionControl != null && dispressionControl.canTakeDamage)
{
dispressionControl.TakeDamage(swordDamage);

// spawn the slash animation object
GameObject slashObject = Instantiate(slashPrefab, col.transform.position, Quaternion.identity);
Destroy(slashObject, 0.42f); // destroy the animation object after 1 second

break;
}
}
}

}

private void LateUpdate()
{
// rotate the sword towards the mouse position
Vector2 direction = mousePosition - (Vector2)transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
targetPosition = targetObject.position;
}

// draw a circle around the sword to visualize its range
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, swordDistance);
}

// detect collisions with enemies
private void OnTriggerStay2D(Collider2D collision)

{
// deal damage to the enemy on collision with the sword
if (collision.gameObject.CompareTag(“Enemy”) && Input.GetMouseButtonDown(0))
{
if (canDamageMultipleEnemies)
{
// Damage all enemies in the trigger area
EnemyController enemyController = collision.GetComponent();
DispressionControl dispressionControl = collision.GetComponent();

if (enemyController != null && enemyController.canTakeDamage)
{
enemyController.TakeDamage(swordDamage);

// spawn the slash animation object
GameObject slashObject = Instantiate(slashPrefab, collision.transform.position, Quaternion.identity);
Destroy(slashObject, 0.42f); // destroy the animation object after 1 second
}
else if (dispressionControl != null && dispressionControl.canTakeDamage)
{
dispressionControl.TakeDamage(swordDamage);

// spawn the slash animation object
GameObject slashObject = Instantiate(slashPrefab, collision.transform.position, Quaternion.identity);
Destroy(slashObject, 0.42f); // destroy the animation object after 1 second
}
}
else
{

}

canSwingSword = false;
Invoke(“ResetSwordSwing”, swordDelay);
}
}

private void ResetSwordSwing()
{
canSwingSword = true;
hasHitEnemy = false;
}
}