Check if entire box collider has air below, instead of just a point on the box?

I’m currently working on a Super Mario Bros remake, and it’s going well aside from a few collision issues.

I’m using Raycasts to determine the situations to apply gravity. In this specific issue, I am spawning a mushroom item, which is a cube with a texture, has a box collider, mesh renderer, a rigidbody, and the script to handle it all. I am using it as a trigger, so a player can pass through it which I think is smoother than grabbing it when they touch the edge of it.

Current mushroom item script: #pragma strict// =========================================================== -

The way the gravity works, is:

  1. Mushroom has a rigid body, constrained on all axes except X (left/right or ‘horizontal’)
  2. If the Mushroom doesn’t have an object within 0.6 of it’s bottom, I assume it is floating and I should apply gravity to it.
  3. Unconstrain Y axis so rigidbody can work it’s magic

The problem is, it doesn’t check if 4 points are all in the air, it only checks for 1 point of the box, which I assume is the very center vertice. I’m wondering if there’s a way to check if the entire bottom of the box (or the 4 bottom vertices), if they are all floating.

So the real issue is that it is triggering the OnTrigger method when the corner of the mushroom catches on the floor it is going to fall off of. This wouldn’t be much of an issue, except I am also checking if the mushroom hits a wall so it can flip directions (If you ever played any mario game, you would know what I mean).


Edit: I’m open to completely rewriting this system too, so don’t be afraid to make a better suggestion than what I have.

Your raycast is going from the center since you use transform.position. As a result, once half of the square has nothing under, it considers that it should fall. Instead of casting from center, cast from side, just add two empty objects at the side edges and cast down, then if both are false (and only if both at the same time), it means it should go down.