Hi,
anybody could please tell me what am i doing wrong in here? is there some setting in the terrain object im missing?
im trying to run this example code in JS:
this is what i get:
if instead if run this code nothing happen at all:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
Terrain terr; // terrain to modify
int hmWidth; // heightmap width
int hmHeight; // heightmap height
void Start () {
terr = Terrain.activeTerrain;
hmWidth = terr.terrainData.heightmapWidth;
hmHeight = terr.terrainData.heightmapHeight;
Terrain.activeTerrain.heightmapMaximumLOD = 0;
}
void Update () {
// get the heights of the terrain under this game object
float[,] heights = terr.terrainData.GetHeights(0,0,hmWidth,hmHeight);
// we set each sample of the terrain in the size to the desired height
for (int i=0; i<hmWidth; i++)
for (int j=0; j<hmHeight; j++){
heights[i,j] = 1000;
//print(heights[i,j]);
}
// set the new height
terr.terrainData.SetHeights(0,0,heights);
}
}
> heights[i,j] = 1000;
Both GetHeights and SetHeights use normalized height values, where
0.0 equals to terrain.transform.position.y in the world space and 1.0 equals to terrain.transform.position.y + terrain.terrainData.size.y in the world space
So, change 1000 to something between 0 and 1.
Notice, however, that terrainData.GetHeight returns a value already multiplied by terrain.terrainData.size.y.
@åperture
the code works great if you use Random.Range(0f,0.01f);
per below
which can be used one time in start, or continual in update
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
Terrain terr; // terrain to modify
int hmWidth; // heightmap width
int hmHeight; // heightmap height
void Start()
{
terr = Terrain.activeTerrain;
hmWidth = terr.terrainData.heightmapResolution;
hmHeight = terr.terrainData.heightmapResolution;
Terrain.activeTerrain.heightmapMaximumLOD = 0;
//}
//void Update()
//{
// get the heights of the terrain under this game object
float[,] heights = terr.terrainData.GetHeights(0, 0, hmWidth, hmHeight);
// we set each sample of the terrain in the size to the desired height
for (int i = 0; i < hmWidth; i++)
for (int j = 0; j < hmHeight; j++)
{
heights[i, j] = Random.Range(0.0f, 0.01f);
//print(heights[i,j]);
}
// set the new height
terr.terrainData.SetHeights(0, 0, heights);
}
}