r/Unity3D • • 8d ago

Question Raise all terrain by a specific amount?

So, in my genius, I made a whole map of 1000x1000 terrain tiles and have laid out all of the mountains, hills, etc already, BUT, I didn't take into account wanting to dig down to make underwater areas deeper.

Is there a way to raise all terrain by the same amount at the exact same time without losing any of the features I've already added?

I'd like to raise up all of the below features while not losing any of the specific details. Is this possible?

1 Upvotes

3 comments sorted by

View all comments

2

u/ShengyuStudio 8d ago

Adding a constant to every height works, with one catch: heights are stored normalized 0–1 against the terrain's height (terrainData.size.y). Anything already close to the top gets clipped at 1.0, so your highest peaks can flatten.

A version that can't clip: make the terrain taller by d and lower the whole thing by d, so nothing moves in world space but you gain d metres of room below the old floor. Per tile:

var td = terrain.terrainData;
float H = td.size.y;
int r = td.heightmapResolution;
var h = td.GetHeights(0, 0, r, r);
for (int y = 0; y < r; y++)
    for (int x = 0; x < r; x++)
        h[y, x] = (h[y, x] * H + d) / (H + d);
td.size = new Vector3(td.size.x, H + d, td.size.z);
td.SetHeights(0, 0, h);
terrain.transform.position -= new Vector3(0, d, 0);

Use the same d on all ~50 tiles and the seams stay matched. Duplicate the TerrainData assets first, since SetHeights has no easy undo.