Colliders Work Sometimes & Don't Work Other Times

I have two different playing fields (prefabs), a wider one for iPhone, and a squarer one for iPad. The iPhone field is a little smaller in area. Other than the size & aspect ratios, the two are identical in all other respects. The character controlled by the player has a collider and a non-kinematic rigidbody, and is moved using rigidbody.MovePosition. The field boundaries (walls) use colliders, no rigidbodies. The exit, which is blocked until the level is completed, is blocked with colliders, same as the walls. In the attached image, you can see two square objects on either side of the exit. They also have colliders which are not shown here, so there are two colliders that block the exit from a side approach.

In both the iPhone and iPad layouts, the walls work, that is, the character stops when it hits them. In the iPad layout, the exit blockers also block the character from passing, but in the iPhone layout, the character slips right through the exit blockers like they’re not even there. The wall colliders work, just not the exit blockers.

I have been over and over and over this, and cannot find any reason why one works and the other doesn’t. Colliders, rigidbodies, the code, everything is exactly the same. Any ideas or suggestions?

rigidbody.MovePosition bypasses collisions. Calling this method force-moves the rigidbody to the specific position, ignoring the results of the physics solver. Depending on when/how MovePosition is called, some collision response may happen or not. Essentially, MovePosition “competes” against the physics solver trying to resolve collisions.

The only way to move a non-kinematic rigidbody while the physics solver can resolve the collisions and frictions properly is using Rigidbody.AddForce / Rigidbody.AddTorque or their variants from FixedUpdate. A convenient and physics-compliant way to move rigidbody-based character controllers is imposing a given velocity like this:

rigidbody.AddForce(targetVelocity - rigidbody.velocity, ForceMode.VelocityChange);

This will impose a velocity to the character, while fully preserving collisions and frictions.

1 Like