Trench Building in Unity — Dev Log

I have been recreating real world locations for a history project and I needed a way to represent slit-trenches. I decided to create a custom Unity editor to help speed up the process.

What does a trench look like?

The finished trench looks like this;

End Product

I have split the work into three main parts;

  1. Dig Trench — Alter the Unity Terrain based on a user defined set of points and dimensions
  2. Add ‘Revetment’ — add paneling on the inside of the trench
  3. Add Back-fill — add a material to fill between the revetment and the terrain.

Digging a Trench

I created a Custom Editor that visualizes where the trench will go. It consists of several features;

The Trench Editor container is represented by a game object with a specific Trench Icon and a label showing the name of the trench. The user then creates ‘Markers’, the names of which are shown as orange labels. The markers are automatically snapped to the nearest terrain height. The editor visualizes the area that will be cut out from the Terrain with green boxes. Once the user is happy with the layout they can press ‘Dig Trench’

Adding Revetment

Once the trench has being dug, the next step is typically to add the revetment. I confess that I have not created a algorithm to the collisions, so initially it is just approximately where the user needs it. I.e. they still have to make the final adjustments.

Before User Adjustments

You can see how the two revetment edges are overlapping or not connecting as you might expect.

After User Adjustment

The user needs to move the revetment around and resize it, but it is much easier than starting from scratch with new prefabs.

Adding Back-fill

That last stage is fill the gaps between the revetment and terrain. This is needed because typically the resolution of the terrain is not finely grained enough to make small adjustments. This means the trench is created wider than specified to ensure required trench width is met. Therefore we need to back-fill the gaps with a couple of game objects per straight trench-run. The auto-generated back-fill again requires User interaction.

Before User Adjustments
After User Adjustments

Where now?

There is still *a lot* of improvements I could make to the editor. I have the majority of the dimensions to mitigate the number of user adjustments required, but currently for my project, the cost-saving ratio does not warrant it. But if I were to make this a Run-Time feature then it would be a different story.

Overall I am quite happy with the result. It is certainly easier than constructing a trench with the core Terrain tools.

If you would like to know more about how I implemented this Editor then please let me know. If there is a demand then I can write a separate series on creating this Terrain tool.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store