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?