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?
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);
}
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 () {
}
}
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