Enemy attack animation issue

Hello, I am trying to add animation on my enemy character, but I don’t know exactly how.

She deals damage when she collides with someone, and I want her to use her animation then, but I tried SetTrigger and SetBool in script, but nothing seems to work, SetBool was the most promising because it actaully played the animation unlike SetTrigger but the bool wont set false after player is out of collision.

Here is EnemyDamage script:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class EnemyDamage : MonoBehaviour
{
    public Animator animator;
    public int damage;
    public PlayerHealth playerHealth;
    public PlayerMovement playerMovement;
    public bool playerInRange;

    public GameObject popUpPrefab;

    void Update()
    {
          animator.SetBool("PlayerInRange", playerInRange)
    }
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            playerInRange = true;

            GameObject popUp = Instantiate(popUpPrefab, other.transform.position, Quaternion.identity);
            popUp.GetComponentInChildren<TMP_Text>().text = damage.ToString();

            if(transform.position.x > other.transform.position.x)
            {
                popUp.GetComponent<PopUpDamage>().hitFromRight = true;
            }
            playerMovement.KBCounter = playerMovement.KBTotalTime;
            if(other.transform.position.x <= transform.position.x)
            {
                playerMovement.KnockFromRight = true;
            }
            if (other.transform.position.x > transform.position.x)
            {
                playerMovement.KnockFromRight = false;
            }
            playerHealth.TakeDamage(damage);

        }
    }
}

Instead of using SetBool or SetTrigger you can use animator.Play("put your animation name here"); If you want the animation to return to idle after it has finished playing you can use an IEnumerator like this:

IEnumerator PlayAnimation()
{
    animator.Play("AnimationName");
    yield return new WaitForSeconds(AnimationTimeLength);
    animator.Play("IdleAnimationName");
}

to call the IEnumerator type this:

StartCoroutine(PlayAnimation());

It works, not as I would want, I mean it works but there are some issues that arent really realated to animation so much. One is realated to animation and that is that animation wont turn to the side player is, I tried transform.localscale but it didnt work. And other issue is that damage is dealt right at the time the player colides and dont know how to delay the damage so it will deal damage at the end of the animation.

For this issue i think the solution is to move playerHealth.TakeDamage(damage); to the IEnumerator like this

IEnumerator PlayAnimation()
{
    animator.Play("AnimationName");
    yield return new WaitForSeconds(AnimationTimeLength);
    animator.Play("IdleAnimationName");
    playerHealth.TakeDamage(damage);
}

not too sure what you mean by this though

The damage is still being dealt on start of the animation for some reason, and what I meant was when player aproaches enemy from right, instead of facing the player enemy will play animation in same direction as if player aproached enemy from left

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class EnemyDamage : MonoBehaviour
{
    public Animator animator;
    public int damage;
    public PlayerHealth playerHealth;
    public PlayerMovement playerMovement;
    public bool playerInRange;
    public bool FromRight;

    public GameObject popUpPrefab;

    void Update()
    {

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {


            GameObject popUp = Instantiate(popUpPrefab, collision.transform.position, Quaternion.identity);
            popUp.GetComponentInChildren<TMP_Text>().text = damage.ToString();

            if(transform.position.x > collision.transform.position.x)
            {
                popUp.GetComponent<PopUpDamage>().hitFromRight = true;
            }
            playerMovement.KBCounter = playerMovement.KBTotalTime;
            if(collision.transform.position.x <= transform.position.x)
            {
                playerMovement.KnockFromRight = true;
            }
            if (collision.transform.position.x > transform.position.x)
            {
                playerMovement.KnockFromRight = false;
            }
            StartCoroutine(PlayAnimation());

        }
    }

    IEnumerator PlayAnimation()
    {

        if (FromRight)
        {
            transform.localScale = new Vector3((float)-0.145491, (float)0.145491, (float)0.145491);
            animator.Play("Banshee_Attack");
        }
        if(!FromRight)
        {
            transform.localScale = new Vector3((float)0.145491, (float)0.145491, (float)0.145491);
            animator.Play("Banshee_Attack");
        }
        yield return new WaitForSeconds(7);

        animator.Play("Banshee_Idle");
        playerHealth.TakeDamage(damage);
    }
}