- Home /
How can I turn multiple objects into a single terrain?
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.
Runtime or edit time? Does the map need to be dynamic (i.e. do the objects change position or size at runtime? Can you give is an image of the objects you are talking about?
There is the brute force method. Define the square area to sample, and then Raycast() down at a regular spacing to produce a sampling of say 513 x 513. The distance of the hit.point from some base gives you the height for the height map (may have to multiply that by a factor). For some layout of the objects you could get away with a Collider.Raycast() ins$$anonymous$$d of a Physics.Raycast().
That sounds perfect! But I've never done something like this before. $$anonymous$$ind sharing some details? (as if you're $$anonymous$$ching a nub cuz you are ;))
Answer by robertbu · Oct 06, 2014 at 05:30 PM
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);
}
}
Your answer

Follow this Question
Related Questions
Is it possible to generate a high-poly spherical model from a mercator-projection heightmap? 1 Answer
Can I set a non-power-of-2 sized heightmap to terrain? 1 Answer
Why rotating the Terrain Collider does not work? 0 Answers
Convert player world position into terrain detail layer coordinates (GetDetailLayer) 2 Answers