I want to convert multiple game objects into a single terrain mesh or heightmap (not in runtime). Object2Terrain definitely won’t work because of the number of objects in my map, so I’m hoping there’s something I can do to create a terrain from a parent object for my game objects. It’s not very important for me to get the individual meshes but at least a heightmap is needed. Note: For technical purposes, I need the single terrain to control all the varying heights of my scene.
Below is a script to get you started. To use:
- Attach the script to a Unity Quad
- Rotate the Quad 90 degrees on the ‘x’ (90,0,0)
- Position and scale the Quad so that it is above your objects. The Quad defines the snapshot taken of the objects below.
- Drag and drop a terrain on the ‘terrain’ variable. Note that if you don’t, it will use the active terrain (if any).
- Set ‘maxDist’. This defines how far the floor of the terrain is from the plane.
- Run the app
Note that the changes to the terrain will persist after the app is run, so you can run the app, then delete the Quad.
So given this layout of objects:
You will get a terrain that looks like this:

Note the cylinder at the top. Unity puts a capsule collider on a cylinder, so the terrain has a capsule instead of a cylinder.
using UnityEngine;
using System.Collections;
public class MapTheTerrain : MonoBehaviour {
public Terrain terrain;
public float maxDist = 1.85f; // distance to floor of terrain
void Start () {
if (terrain == null) terrain = Terrain.activeTerrain;
if (terrain == null) return;
int size = (int)terrain.terrainData.heightmapResolution;
float[,] heights = new float[size,size];
float delta = 1.0f / size;
Vector2 testPos = new Vector2(-0.5f, -0.5f);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
RaycastHit hit;
Vector3 pos = transform.TransformPoint(testPos);
if (Physics.Raycast(pos+0.001f * Vector3.down, Vector3.down, out hit)) {
float dist = Mathf.Clamp(hit.distance, 0.0f, maxDist);
heights[i,j] = (maxDist - dist) / maxDist;
}
else {
heights[i,j] = 0.0f;
}
testPos.x += delta;
}
testPos.x = -0.5f;
testPos.y += delta;
int row = i + 1;
}
terrain.terrainData.SetHeights (0,0,heights);
}
}
