How to make attacker ignore defender’s death animation /2

Hello,
I am here to ask for the same question, after health <= 0 the death animation is playing but the attacker does not pass until my defender destroys completely himself. I tried to set boxcollider2d enable false but it didnt work then i tried to disable the boxcollider on the death animation of defender still its not working.

  [SerializeField] float health = 100;
    Animator animator;
   
    void Start()
    {
        animator = GetComponent<Animator>();
    }
  
    public void DealDamage(float damage)
    {
        health -= damage;

        StartCoroutine(color());
        if (health <= 0)
        {
            GetComponent<BoxCollider2D>().enabled = false;
            StartCoroutine(die());
        }
    }

    IEnumerator die()
    {
        animator.SetTrigger("isDying");
        yield return new WaitForSeconds(3f);
        Destroy(gameObject);
    }

    IEnumerator color()
    {
        gameObject.transform.Find("Body").GetComponent<SpriteRenderer>().color = Color.red;
        yield return new WaitForSeconds(0.3f);
        gameObject.transform.Find("Body").GetComponent<SpriteRenderer>().color = Color.white;
    }

Thanks for replies.

Hi Can,

Disabling the BoxCollider2D is a good idea, and I’m surprised that it did not work. Does the defender have got more than one collider attached? How long is the “attack” animation of the enemy? Did you add Debug.Logs to the relevant methods to see when which code block is executed?

I’m wondering if disabling the collider calls OnTriggerExit2D. :confused:

1 Like

Only one collider

The right side is my defender’s inspector and the left and the bottom is my attacker’s attack animation clip.

If i dont use a die animation everything works very well but when i use its not working. Debug.log got me here to the Health script.
There is one thing also, in the game, i am pausing when defender at the die animation and then trying to inactive all the defender as on the image which i upload but attacker still try to kill my defender :smiley:

Why is the game object disabled? If you want to play the “die” animation, the game object needs to be active.

Yeah but why is still attacker trying to attack,
i have no idea, i tried everything thats why :smiley:

As aforementioned, you need to check with a Debug.Log whether OnTriggerExit2D works or if OnTriggerStay2D is still being invoked. Also try to figure out which object is triggering the enemy’s collider. You can get this data via the argument that was passed on to the OnTrigger method.

This topic was automatically closed after 14 days. New replies are no longer allowed.

Privacy & Terms