Post news RSS Tunneling through the Terrain (Limitations)

How I overcame the limitations of Unity's terrain system to create traversable tunnels through the terrain.

Posted by on

Sometimes it's the small things that seem to make a large impact. In this case, I have just opened up a world of potential secrets and environment designs that will support traveling through tunnels. Now this may not seem like a big deal given how simple of a concept it is. However remember that Unity only supports heightmaps for terrain; meaning you cannot punch holes in it or mold it upside down. Voxels can be used, however they are ~10x more performance intensive, and frankly they always appear blocky to me. As I'm not making Blast Corps meets Minecraft, this just wouldn't suffice so I set out on "simulating" tunnels. Before I get in to the technical details, here is the final product:

The concept at play here is simple: disable the vehicle -> terrain collisions when inside the tunnel. Originally I had simply disabled the terrain collider upon entering the tunnel trigger zone, however I realized that this would cause the vehicles (and main semi) to fall through the ground upon doing so. Instead I decided to refine this to be a bit more specific and created a new layer called "Ignore Terrain Collision". As I already had the Terrain on its own layer, it was just a matter of preventing collisions between the two layers in the project physics settings.

Naturally changing this and then adding the vehicle to the new "ignore terrain layer" would cause the vehicle to just fall through the ground anyway, so instead I would toggle the vehicle to the layer upon entering the tunnel. The caveat here is that there needs to be another collider (road for example) beneath the vehicle or else we would fall through the terrain in the tunnel anyway. The same goes for the surrounding tunnel walls or the player could just drive around beneath the terrain. Keeping the road/base and the surrounding tunnel walls on a different layer that collides with the "ignore terrain collision" layer pretty much prevents any unwanted travel once in the tunnel; as it should be.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.