0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Try changing the Rigidbody's Collision Detection from Discrete to Continuous \$\endgroup\$ – Chris McFarland Apr 29 '15 at 0:29
  • \$\begingroup\$ Looks like both my character and NPC were both already set to Continuous collision detection. \$\endgroup\$ – eternal Apr 29 '15 at 0:55
  • \$\begingroup\$ Did you try making your house's collider thicker? \$\endgroup\$ – tbkn23 Apr 29 '15 at 4:39
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Joel Apr 29 '15 at 5:10
  • \$\begingroup\$ 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! \$\endgroup\$ – eternal Apr 29 '15 at 17:01
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.