Player getting stuck in tilemap after knock by enemy in 2D topdown game

So I have a player and enemies (slimes) using rigidbody 2D dynamic with box colliders2Ds (Is Trigger). I have implemented a knockback effect for the player using rb.AddForce() when colliding with the enemy slime.
I also have tilemap collider using composite collider (NOT Is Trigger) and a static rigidbody. The player does not get stuck or clip through the tilemap with control inputs (keyboard) nor can the enemy move through the tilemap. However, for some reason when the player is too close to one of the tilemap objects and collides with the enemy, the knockback force just launches the player inside the tilemap object, which makes the player stuck basically.

This is the onTriggerenter2D function in the enemy’s script giving the knockback force to the player

public void OnTriggerEnter2D(Collider2D collision)
    {
       
        if (collision.tag == "GameController") {

            return;
        }
        else if (collision.tag == ("Player")) 
        {
            //Debug.Log("Player touched");
            PlayerController player = collision.GetComponent<PlayerController>();            
            //Vector3 PlayerPos = collision.transform.position;
            Vector2 Direction = (collision.transform.position - transform.position).normalized;
            Vector2 KnockBack = Direction * KnockBackForce;
            Vector2 deathKnockVec = Direction * deathKnock;

            if (player != null)
            {
                
                    player.OnHit(damage, KnockBack);

               
                 
            }
        }
    }

Is there a way to call the tilemap’s collider and nullify the player’s velocity upon collision to prevent the clipping?

What you need to do is to reflect off the slime that you hit. You can adapt this code to get the new direction for moving away. It works by getting the normal (perpendicular) of the slime and then reflecting around that. I added a force to move my sample circle bouncing off a wall. The variable called direction is simply the direction that my circle was traveling in before it hit the wall. Hope it makes sense…

using UnityEngine;

public class Bounce : MonoBehaviour
{
    Rigidbody2D rb;
    Vector2 direction = new Vector2(2, 1).normalized;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.AddForce(direction * 3, ForceMode2D.Impulse);
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        Vector2 normal = collision.contacts[0].normal;
        Vector2 newDirection = Vector2.Reflect(direction, normal);
        rb.AddForce(newDirection * 3, ForceMode2D.Impulse);
    }
}

Hi!

You could add both a non-trigger collider and a script to your tilemap in order to detect collisions with the player. The code would probably look something like this:

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!collision.gameObject.CompareTag("Player")) return;
        Rigidbody2D rigidBody;
        if (!collision.gameObject.TryGetComponent(out rigidBody)) return;
        rigidBody.velocity = Vector2.zero;
    }

I hope this helps!