flatten Terrrain under object

Hey

I need to flatten my terrain under the the objects I imported (roads & buildings) so that it’s flat around it. The problem is that the roads aren’t flat so I kind of wanted to ray cast ups/down in order to get the right terrain hight at those points. But I am also unable to code it myself.

It would be great if somebody could help me out with some code are asset!

This is just a very quick and not optimized script to give you an idea.
Place terrain at coordinates 0,0,0; place a FPS Controller on it, attach script to FPS Controller and walk a while. Then press H.
Of course, you have to put some more logic to create roads on this way but this is an example how you can manipulate terrains data.

Flatter.cs:

using UnityEngine;
using System.Collections;

public class Flatter : MonoBehaviour {
	
	TerrainData terrData;
		
	float[,] heightmapData; // prepare a matrix with terrain points
	float ratio; // ratio between player position and terrain points
	
	// Use this for initialization
	void Start () {
		
		terrData = Terrain.activeTerrain.terrainData;
		int terrRes = terrData.heightmapResolution;
		Vector3 terrSize = terrData.size;
		
		heightmapData = terrData.GetHeights(0, 0, terrRes, terrRes); // heights we will change during of walking
		
		ratio = terrSize.x / terrRes;
		
		Debug.Log ("heightmapData="+heightmapData +" terrRes="+terrRes+" terrSize="+terrSize + " ratio="+ratio);
	}
	
	// Update is called once per frame
	void Update () {

		int terrainPointX = Mathf.CeilToInt(transform.position.x / ratio);
		int terrainPointZ = Mathf.CeilToInt(transform.position.z / ratio);
		
		Debug.Log (" terrainPointX="+terrainPointX+" x terrainPointZ="+terrainPointZ +": set height to 0.0");
		
		heightmapData[terrainPointZ,terrainPointX ] = 0.0f; // move terrain point to 0 (example)

	}
			
	
	void OnGUI() {
		if (Input.GetKey(KeyCode.H)){
			Debug.Log("Saving");

			terrData.SetHeights(0, 0, heightmapData); // save terrain heights back
		}
	}
}

Thanks. That’s just what I needed. I guess that if I kind of throw a grid over ever the entire scene and ray cast vertical collisions, that then I should be able to adjust the entire terrain by that.

Could somebody please make this comment? Thanks :wink: