- Home /
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: http://pastebin.com/jCjZiaYX
The way the gravity works, is:
Mushroom has a rigid body, constrained on all axes except X (left/right or 'horizontal')
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.
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).
Thanks!
Edit: I'm open to completely rewriting this system too, so don't be afraid to make a better suggestion than what I have.
Any chance you could post your code here as a .zip file. $$anonymous$$y internet connection is not the best and the site you posted on is not responding well at all. I will have a look at your code and see if I can help you if you do. Thanks.
fafase's solution is a good one!
Answer by fafase · Oct 15, 2014 at 03:36 PM
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.