- Home /
please explain Terrain.SetHeights
I'm trying to set terrain heights by code but i just don't get the Terrain.SetHeights command. Can someone please explain it so even a noob can understand it (best with example)? There are NO examples in the code reference...
How can I create the needed attribute "float[,]"?? Is it even possible via javaScript? I read somewhere that this can only be done in C#, but I thought that the programming language doesn't matter in Unity...
I could get the current heights via Terrain.GetHeights. How can I now edit these settings?
Answer by maikkel · Oct 06, 2010 at 11:17 AM
I got it now, Here's a test script to randomize heights and reset them, with buttons. Attach the script to the Terrain:
var terrain : Terrain; var tData : TerrainData;
var xRes : int; var yRes : int;
var heights : float[];
function Start () { terrain = transform.GetComponent(Terrain); tData = terrain.terrainData;
xRes = tData.heightmapWidth;
yRes = tData.heightmapHeight;
terrain.activeTerrain.heightmapMaximumLOD = 0;
}
function OnGUI() {
if(GUI.Button (Rect (10, 10, 100, 25), "Wrinkle")) {
randomizePoints(0.1);
}
if(GUI.Button (Rect (10, 40, 100, 25), "Reset")) {
resetPoints();
}
}
function randomizePoints(strength:float) { var heights = tData.GetHeights(0, 0, xRes, yRes);
for (y = 0; y < yRes; y++) {
for (x = 0; x < xRes; x++) {
heights[x,y] = Random.Range(0.0, strength) * .5;
}
}
tData.SetHeights(0, 0, heights);
}
function resetPoints() { var heights = tData.GetHeights(0, 0, xRes, yRes);
for (y = 0; y < yRes; y++) {
for (x = 0; x < xRes; x++) {
heights[x,y] = 0;
}
}
tData.SetHeights(0, 0, heights);
}
Answer by Mike 3 · Oct 05, 2010 at 11:44 PM
it's possible, but you need a c# script to create the 2d array in the first place
What you can do is grab the MultiDimArray script from the wiki ( http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays ), then fill int he values yourself
The array itself is basically similar to a greyscale texture - you set a 0->1 value for each item in it, and it maps to an x/y coordinate, e.g. yourArray[x,y] = 0.5;
If you just needed to grab the original heights and change them, you'd just use var by itself without a type, e.g. var heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 10, 10); then access them as above
thanks for the tips, i also got some help in the forums, i think I got it now.
Answer by eurasian · Oct 25, 2014 at 04:25 PM
This is the same code in C#, for those interested:
using UnityEngine;
using System.Collections;
public class RandomizeHeights : MonoBehaviour {
Terrain terrain;
TerrainData tData;
int xRes;
int yRes;
float[,] heights;
void Start () {
terrain = transform.GetComponent<Terrain>();
tData = terrain.terrainData;
xRes = tData.heightmapWidth;
yRes = tData.heightmapHeight;
}
void OnGUI() {
if(GUI.Button (new Rect (10, 10, 100, 25), "Wrinkle")) {
randomizePoints(0.1f);
}
if(GUI.Button (new Rect (10, 40, 100, 25), "Reset")) {
resetPoints();
}
}
void randomizePoints(float strength) {
heights = tData.GetHeights(0, 0, xRes, yRes);
for (int y = 0; y < yRes; y++) {
for (int x = 0; x < xRes; x++) {
heights[x,y] = Random.Range(0.0f, strength) * 0.5f;
}
}
tData.SetHeights(0, 0, heights);
}
void resetPoints() { var heights = tData.GetHeights(0, 0, xRes, yRes);
for (int y = 0; y < yRes; y++) {
for (int x = 0; x < xRes; x++) {
heights[x,y] = 0;
}
}
tData.SetHeights(0, 0, heights);
}
// Update is called once per frame
void Update () {
}
}