I have a level where I knockback the player when they enter a box collider, using force to push them, and the player can sometimes get shot through the level walls and floor if the knockback object it hit at some angles angle. I do not want this to happen. How can I keep the player from getting thrown through the walls?
Here is the knockback script
public float force = 3;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
Vector3 dir = (col.contacts[0].point - transform.position).normalized;
col.rigidbody.AddForce(dir * force);
}
}