Unity Terrain Script

here is a script for creating terrains from height maps. just pop in the terrain, your height map, and run it bam, your terrain looks like your height map. just copy paste into a blank script

    using UnityEngine;
using System.Collections;

public class TerrianScript : MonoBehaviour {

    public bool RunIt;
    public Terrain DaTerrain;
    private float[,] Heights;
    public Texture2D DaPic;

    //note this must be done while the game is running inorder to make it go

	// Use this for initialization
	void Start () {

	// Update is called once per frame
	void Update () {


    private void DaMakers()

        // a good place to get a height map from the website below
        //Warning - in older verisons of unity an alpha transparency will throw an error if imported, to fix, simply open and save in MS paint, or other wise remove alphas.

        Texture2D copy = duplicateTexture(DaPic);

        //this gets the terrian height data

        // WARNING - if the Terrain data is wider than your picture it will throw an error

        DaTerrain.terrainData.heightmapResolution = copy.width;

       Heights = DaTerrain.terrainData.GetHeights(0, 0, DaTerrain.terrainData.heightmapWidth, DaTerrain.terrainData.heightmapHeight);

        for (var i =0;i< DaTerrain.terrainData.heightmapWidth; i++)
            for(var j=0;j< DaTerrain.terrainData.heightmapHeight; j++)
                //the hieghts range from 0 to 1 and the tallest point will be 
                Heights[i, j] = copy.GetPixel(j, i).grayscale;


        DaTerrain.terrainData.SetHeights(0, 0, Heights);
        RunIt = false;

    //this makes whatever height map you are importing reasonable
    private Texture2D duplicateTexture(Texture2D source)
        RenderTexture renderTex = RenderTexture.GetTemporary(

        Graphics.Blit(source, renderTex);
        RenderTexture previous = RenderTexture.active;
        RenderTexture.active = renderTex;
        Texture2D readableText = new Texture2D(source.width, source.height);
        readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
        RenderTexture.active = previous;
        return readableText;
1 Like