Platformer floor/ceiling collision detection flakiness

For my platforming movement I need to keep track of two simple checks (for now):

  • If the player is standing on a block then they’re counted as on the ground and the animation changes, various flags are updated, etc
  • If the player hits their head on the ceiling then their jump is cut short early

The character itself uses a box collider with simple physics. My solution was to create head and feet trigger colliders that poke slightly out the top/bottom of the main collider and are slightly thinner, so that in theory they only collide against ceilings/floors. See diagram: Imgur: The magic of the Internet

However I find that sometimes these triggers are hitching against walls as well - i.e. I jump diagonally into a wall and sometimes the player box collider will briefly slip inside the wall’s box collider, triggering a collision on the head or feet collider and screwing up the physics.

Anybody have any idea why this happens? How do I guarantee that two box colliders will definitely never overlap at all, so I can prevent inappropriate collisions with the triggers? Or is there a smarter way to do this?

Since you are using trigger colliders, physics alone does not make “collisions” happen. You should check what type of collider you bumped into, usually by using tags.

Yeah I’m doing that, but the tag is the same whether it’s a “floor” or a “ceiling” (i.e. it could just be a floating platform). So for example if the head trigger collides with any block it will stop the jump, but I don’t want to do that unless it actually is a block above the character.

In the case of a jump, the vertical velocity should usually be above zero. You could check for that.