- Home /
Make terrain flat at runtime
Are there any simple ways to do this? I looked around a lot and couldn't find anything helpful. I'm not very experienced with unity terrain and I need a quick way to make it flat at runtime, through code. Thanks in advance!
Answer by Positive7 · Aug 08, 2015 at 10:38 PM
using UnityEngine;
public class test : MonoBehaviour {
public Terrain terrain;
int terrainX;
int terrainY;
void Awake(){
terrainX = terrain.terrainData.heightmapWidth;
terrainY = terrain.terrainData.heightmapHeight;
var heights = terrain.terrainData.GetHeights(0, 0, terrainX, terrainY);
for (int x = 0; x < terrainX; x++) {
for (int y = 0; y < terrainY; y++) {
heights[x,y] = 0;
}
}
terrain.terrainData.SetHeights(0, 0, heights);
}
}
Answer by peturhelgi · Jul 07, 2019 at 06:12 AM
A bit late response, but I ran into this problem and ended up here. If you set the terrain's heightmapResolution, the terrain data is lost, i.e. it gets flat. I don't know what it does with its properties, but it gets the job done.
terrain.terrainData.heightMapResolution = yourValue;
Just know that it rounds it to the nearest (correct me if I'm wrong) resolution of (2^n)+1;
Your answer
![](https://koobas.hobune.stream/wayback/20220611235002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a flat path down terrain 1 Answer
Terrain Generator Producing Completely Flat Results. 1 Answer
LowPoly/Flat Unity terrain shader? 1 Answer
how to remake a terrain flat ? 1 Answer
Make a simple tree 1 Answer