Preventing characters falling through world when altering terrain at runtime

Is there any way I can detect when a run-time terrain edit causes a player to fall through the world, and bump the player back above ground?

I’m allowing players to raise and lower the heightmap of the terrain in game, whilst using a first person camera. This means they can potentially raise the terrain below their feet, which currently causes them to fall through the world unless they jump first.

I was considering keeping a global list of all players, and after every terrain edit, for every player do a raycast from the sky above him, vertically down to the terrain, then comparing the hit Y position to the players Y position. However I’m unsure if this is scalable if I want to in future check for other objects such as enemies, trees, objects on the ground, etc.

I have read about physics spheres, but the sphere would have to be pretty huge to account for the case of one very low point and another very high point next to it, then the player on the slope between them.

What’s the best method of detecting and bumping players back up?

1 Answer

1

You could use TerrainData.GetInterpolatedHeight to find new position and move any objects that are below their corresponding position. That will be more efficient than raycasting.

Do your characters use rigidbodies? If they do, instead of just manually assigning the bump, do that and then use AddForce to make the player jump in the air a bit. This way it makes sure the player is entirely out of the terrain, and it lets normal collision take over when the player lands.