# Unity Rigidbody gets pushed through collider by another rigidbody?

I'm setting up the interior of a house in a 2D platformer. Inside the house is an NPC with a box collider and rigidbody attached, and the house is boxed in by four edge colliders. My character also has a collider and rigidbody.

My NPC walks back and forth randomly, but when he pushes my character into the wall, the force of the push takes priority over the house's edge collider and my character gets pushed outside.

Is there any way to give priority to the house's collider somehow, without removing the NPC's rigidbody?

• Try changing the Rigidbody's Collision Detection from Discrete to Continuous – Chris McFarland Apr 29 '15 at 0:29
• Looks like both my character and NPC were both already set to Continuous collision detection. – eternal Apr 29 '15 at 0:55
• Did you try making your house's collider thicker? – tbkn23 Apr 29 '15 at 4:39
• Also check if the NPC's rigidbody isKinematic, if so it will ignore other bodies around it and they will be forced to do (sometimes unnatural) things to get out of the way. – Joel Apr 29 '15 at 5:10
• The NPC wasn't set to isKinematic, but increasing the house collider size did it. Seems like an obvious solution, but I had previously tried using thin rectangular box colliders for the edges and it didn't work so I thought that wasn't the right approach. Thanks! – eternal Apr 29 '15 at 17:01

Unity's collision detection is discrete so if an object passes through another object within 1 simulation frame, the collision will not be detected. If the collision is already detected, however, forces should push the objects apart and prevent a pass-through.

You can adjust the physics settings to make Unity's physics respond more accurately to collisions, and thus make pass-throughs less common. There are several settings you might consider changing:

Fixed Timestep: This field in the Time Manager controls the rate at which the simulation is run. A smaller value means there is less time between every simulation frame, thus less chance a collision will not be detected.

Collider size: The bigger the collider, the more an object will have to move to pass through and dodge the collision detection.

Adaptive Force: Found in the physics manager, setting this value to true will make force transfer more realistic so instead of just hitting a brick wall and overlapping, the force will be transferred to the brick wall.

Solver Iteration Count: This is basically how many times the collision resolution algorithm is run per simulation frame. The more times, the more accurate - the less passthroughs.

Min Penetration For Penalty: If 2 objects overlap more than this magnitude, collision forces will be applied. The lower this value, the less chance there will be a pass-through.

Continuous collision detection: I don't recommend this for, well, anything because it takes a huge toll on performance but this will guarantee all collisions will be detected. Usually if something needs continuous collision detection (like a bullet), it can be handled with other means like raycasting.

Note that all of these settings have the potential to decrease performance so you should thoroughly tailor them to your game. You can find more information on these settings in Unity's documentation: http://docs.unity3d.com/Manual/comp-ManagerGroup.html.

In Unity 2020 go to Edit -> Project Settings -> Physics to make essential tweaks to Physics settings.

For me, what worked was to change the Default Max Depenetration Velocity. This is like the force that the collider pushes back against the item colliding into it. My issue was that I used a Rigid Body vs a Static Collider (Capsule). Because the Default Max Depenetration Velocity was set to just 10, my high-speed object was passing through the collider. I changed to 1000 and the collider became much more aggressive towards the invader :). All good now.