Managing collisions on multiple moving platforms when they are making contact

Hi!

I currently am creating blocks that can move in any of four directions and will sometimes be at rest and sometimes move depending on their behavior. The blocks would also be solid on all sides for the player. For example, I’d like blocks that could move towards one another and make platforms for the player to walk on, or for platforms to be able to fall like Donut Blocks in Mario.

Example:


Blocks move together


creates “bridges” for player to walk across

The problem here lies in that managing collisions on these blocks has been no easy task. A player walking across them causes all sorts of weird collisions. I know here’s been a lot of virtual ink spilled about Ghost Vertices and all of the unwanted collisions they cause.

There isn’t really an easy solution I’ve found, and even less on when platforms have their own independent rigidbodies for moving. It seems one of the best fixes is using a composite collider 2d parent of all the blocks to get a collection of all the individual colliders, but this doesn’t exactly work in my scenario because since all the blocks have their own rigidbodies, it conflicts with the rigidbody being used for the parent composite collider.

Has anyone here experienced a similar problem and found a solution? It’s not a very uncommon feature in platformers so I’m surprised I’m having such a difficult time hunting down a fix!

The only way you can avoid such ghost collisions is to ensure you use physics edge primitives which is what is used by the CompositeCollider2D when using Outline geometry mode.

Given that, you can use a single EdgeCollider2D for the top surface (assuming you only interact with that) and add the Start and End adjacent vertices to it too. These start/end vertices are what allow the edge primitive to “join” to other edges (such as when you use the composite) as it gives the solver a hint as to how it should solve contacts.

The adjacent start/end would extend another block size to its left/right respectively.

If you also interact with the sides/edge then you can either do a little clever change of the edge to extend down the sides depending on what blocks are aroud it or you could do the simpler way which is to add an additional BoxCollider2D that covers the block but is slightly below the EdgeCollider2D on the top; ensure it’s far enough away so that you never interact with it from the top and maybe use Continuous Collision Detection on your character(s).

This is how the character here is able to move across the following surface without any addition code:

Hope this helps.