I'm using Unity Engine. I see the common way to check if player is on ground is shooting a Raycast:

How to solve the ground check problem?

How to check if grounded with rigidbody

I have a simple scene with Unity's terrain for ground (Game Object > 3D Object > Terrain). Imagine you have a lot of NPCs (like GTA), is it possible to check a character on ground without Raycast? Do you have to run loop to ray cast check each NPC?

  • \$\begingroup\$ When do you need to check if an NPC is on the ground? Is there a chance you put that NPC somewhere else that isn't the ground? In something like GTA, the NPCs don't tend to leap or jetpack around, so there's no particular need to check their grounded state unless they've been flung by an explosion. What kinds of scenarios in your game lead to an NPC leaving the ground? When we understand those situations, we might be able to help restrict these ground checks to a small number of cases rather than the whole pack. \$\endgroup\$
    – DMGregory
    May 21 '18 at 11:24
  • \$\begingroup\$ @DMGregory: my scenario exactly like GTA (consider my game is a GTA clone), but I vaguely remember that NPCs in GTA did react to falling when they walk off the ground. But as you suggest, I think checking ground state only when special case appears could be a good idea. Thanks. \$\endgroup\$
    – 123iamking
    May 21 '18 at 12:27

My advice for pedestrian-type NPCs would be: don't.

For player characters, we often want to give them full freedom of motion to run and climb and leap or launch themselves around all over the place. That usually involves some form of physics simulation, and checking to see whether they've landed/caught a ledge/gobs of other complex motion states and contact events.

For crowds of pedestrians who mostly stand around and stick to the pathways, this would be excessive. Rather than simulate them physically and check if they're on the ground, we can instead drive them along the ground by construction.

This is commonly accomplished by a navmesh - a polygon mesh representing the walkable/navigable surface of your world. We can use it for more than just AI pathfinding: since the navmesh is constructed to closely follow the terrain, picking a point on the navmesh automatically gets us a grounded position. We don't need to check if the NPC is in the air, because we never build navmesh floating out in the air.

In practical terms this means removing or disabling rigidbody physics from your NPCs, and letting the navmesh navigation component drive their motion.

Now, there might still be edge cases where you need more player-like physics & motion: your NPC gets pushed off of the navmesh, or gets thrown into the air by an explosion, or is standing on a dynamic object like a drawbridge when it starts to open. For these situations you'll want to switch to a dynamic/physics-navigating state for the small collection of NPCs who need this special attention, while still letting the bulk of your NPCs use the cheaper, pure-navmesh route.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .