How to Stop Enemies from Falling through Terrain for Good

Hello everyone - I’ve had a problem that’s plagued me for too long - enemies falling through terrain. I’ve Googled and followed a bunch of suggestions and nothing has worked 100%. The enemy still falls through the ground occasionally.

The problem is that I’ve done all kinds of funky stuff with the movement: they use Navmesh which is guaranteed never to fall through terrain buuuuut, I turn that off on the enemy and use physics for knockback when they get it, I use root motion to control the navmeshagent speed to move, and also the enemy has an attack that turns off the navmeshAgent and uses a dynamic point of impact (using Animator.MatchTarget).

I’m guessing either one or all of these things cause the enemy to fall through the ground. I’m not sure what the reason is but my best guess is because the enemy’s root motion animation forces them in a position where their collider is under the terrain, then they start falling because physics and gravity are on and keep falling until my code turns their navMeshAgent back on and they snap back into place on the ground.

Most of the suggestions I’ve seen to fix the “falling through terrain” issue has to do with an object that’s moving too quickly for the physics engine to detect the collision with the ground and I don’t think that’s my issue here since I’ve followed all of the suggested solutions for that and it hasn’t helped much.

Is there any way I can guarantee that an object stays above ground always?

I know you’ve probably tried this…but here goes…Have you tried adding a Rigidbody with “Is Kinematic” to the terrain?
Don’'t get too mad at me !!! I know you must have :slight_smile:
Apart from that I would just check my collision envelopes etc.
Folks MUCH better than me will no doubt answer you better.
All the best .

1 Like

Yes, i’ve tried that
What do you mean by collision envelopes?

I’m throwing a suggestion from the top of my head…

Maybe add some cubic/spheric collider that easy to calculate for their feet?
If the mesh is to hard to calculate…

By the way, I’m pretty sure that there are tutorials online on how to deal with colliders through code.

I’m not sure what you mean by collider at their feet - they already have box colliders, not mesh colliders.

I forgot to mention that I already found a script online that uses raycasts to check for collision.
It’s called “DontFallThroughFloor” or something. Even that doesn’t work 100% of the time.

why don’t you change the way your enemy attacks??? Maybe that is the reason. Maybe the only reason that your enemy falls through the terrain is that you turn off the nav mesh. I have a good tutorial for enemy ai if you want. Here is the link

P.S if you don’t know what a collision envelope is, I would google it

I turn off NavMesh so I can have physics based knockback and root motion controlled animations.
If I don’t turn it off, the attacking would look much more static, with no knockback effects and no dynamic animations which I spent a lot of time adding to the game and making look good.

I literally Googled “collision envelopes” unity and the first result is me asking what a collision envelope is. The rest are local car collision centers in my area.

The only other result I could find is by Googling “collision envelope” unity and it’s a post by vertexx (the guy who brought it up in this thread as well) recommending the same thing to someone else without explaining what he means by that.

I can only conclude that “collision envelope” is a term he made up himself of a specific technique he’s thinking of (that may or may not have a more official, different term for it) but he refuses to explain what he means so that anyone else can understand what he’s talking about.

I saw the same video and used the same code , but still my sheep falls through ground or floats off

if you downloaded the terrain model, then maybe it didn’t come with a mesh.
if it didn’t come with a mesh mesh collider wont work. and if you do have a mesh and mesh collider then maybe the mesh wasn’t properly created by the creator of the model and there are some holes in the ground. try using a plane to see if all collider like on the plane are bugged or if its just your terrain.