Allowing player to pass through a gameObject when condition fulfilled that appears on collision

My player is a circle, and my enemy is a square.
The enemy has a box collider2d and some HP, reducing each time the player collides.
I want my player to bounce back each time he hits the enemy except when the enemies HP drops to 0.
In this case the player should pass through the enemy (and the enemy will be destroyed).

The only way I could imagine is a second collider (trigger) that is bigger than the actual collider. but this would result in either the player would not bounce correct because the collider is too small or the trigger would be too big and the player could hit the enemy by crossing the trigger.

Is there a way to use only one collider and check on collision if the player willl pass through or bounce back?

If you’re using Rigidbody with body type kinematic, then here’s a solution:

using UnityEngine;

public class Test : MonoBehaviour
{
    float pushForce = 10f;

    protected virtual void OnCollisionEnter2D(Collision2D collision)
    {
        var hitObject = collision.gameObject;

        var box = hitObject.GetComponent<Box>();
        if (box != null)
        {
            if (box.HP > 0)
            {
                Vector2 playerPosition = transform.position;
                Vector2 boxPosition = hitObject.transform.position;

                //playerPostion and boxPosition might not be the center of your object, so you might want to try Collider2D.bounds.center to get appropriate centers.
                

                //Push back force Vector:
                Vector2 pushVector = (playerPosition - boxPosition).normalized * pushForce;
                //use this force to apply it to your player, and calibrate pushForce to get the desired push
            } else
            {
                box.ReceiveDamage();
            }
        }


    }
    
}
public class Box : MonoBehaviour
{
    public float HP { get; set; }

    public void ReceiveDamage()
    {
        HP--;
    }

}

The OnCollision will run after a collision already happened. So you can’t really do that with physics and dynamic rigidbodies with one collider. For collision already had happened. You would need to apply your own forces to override collisions effects. Probably doable but can cause issues with interpolation, etc. In this case you can also try to cache velocity at start of fixedupdate and reset it in lateupdate when your condition happend. Maybe you can resolve interpolation issues in that way.

As for other options you either need some sort of kinematic bodies or trigger setup, or if you want to use physics you need to anticipate collision before it happens by second collider trigger, as you said. And then you can try with that trigger to disable collider to not cause collision. But still you would need to run your health logic in the trigger not collider, because if you disable collider you won’t get OnCollision invocation, so if you’d put any logic there it wouldn’t die on last collision.

The issue here is that you can jump on collider and trigger at the same time. If this would run in the same fixedUpdate you still have 1 problem.