- Home /
How would I flip or mirror Terrain data during runtime?
I am trying to make a simple endless terrain illusion. Terrains don't scale at all with their transforms though, so I can't just use transform.scale(-1,0,0) to pull off making the clones needed for wrapping. So I'm now trying to create the terrain clones during runtime.
But I can't seem to get anything returned from terrain.terrainData.GetHeights(0,0,w,h); If I debug.log that info, I just end up getting "System.Single[,]" instead of the array data. According to the docs, from what I understand that should be returning a 2d array of the terrain data, and certainly should not be returning an empty array, in any case.
Here's the full code below. All I get when running this is the debug returning System.Single[,], followed by a IndexOutOfRangeException error. (The indexoutofrange is presumably because I have not gotten the data correctly in the first place) :
//variables
var terrainBase : TerrainData; //original terrain data, editor set.
function Start () {
terrain2A = Terrain.CreateTerrainGameObject(terrainBase);
//scale 2A along the Z axis
var t2ATerrain = terrain2A.GetComponent(Terrain) as Terrain;
var h = t2ATerrain.terrainData.heightmapHeight;
var w = t2ATerrain.terrainData.heightmapWidth;
var oTerrain = t2ATerrain.terrainData.GetHeights(0,0,w,h);
Debug.Log(oTerrain);
t2ATerrain.terrainData.SetHeights(0,0,FlipTerrainX(oTerrain,w,h));
}
function FlipTerrainX(originalArray : float[,],heightmapX:float,heightmapY:float) {
var nArray = new float[heightmapX,heightmapY];
//Debug.Log(originalArray);
//Debug.Log(nArray);
for(var x = 0;x < heightmapX;x++) {
for(var y = 0;y < heightmapY;y++) {
//flip the x, but leave y matching
nArray[heightmapX - x,y] = originalArray[x,y];
}
}
return nArray;
}
I'm sure I am just missing something simple here.
All I want to do is be able to loop and process the array of heightmaps to mirror the terrain for the other 4 versions needed to loop it forever. This error is a bit over my head, however, and searches are not being very helpful since the information I'm getting is very conflicting on if this should even work.
Does anyone have a reference for how terrain arrays are structured? I really need to be able to manipulate the terrain data to make any headway here.
At this point, I'm considering spending a few weeks just coding my own terrain, but since this is for android and I'm not the best coder I'd much rather use the unity one. I also have a few tools that edit regular Unity terrain, and hoped I could make use of those.
I've tried:
-starting a completely new project -just creating an empty terrain -attaching the above code to an empty game object in the scene
I STILL get nothing but a blank array in return with the GetHeights() function, and the array parser still overloads.
I'd report a bug to Unity, but I don't want to report something if I'm just using it wrong.
All the other functions of the terrain are working like calling a single point's height or setting a single data point during runtime.
If I knew the structure of the terrain data, I could probably just loop the data using the single data point functions. But since the documentation just says "Get an array of heightmap samples.." without describing the structure of the array, I have no idea how to write such code.
Answer by hav_ngs_ru · Dec 16, 2014 at 08:02 AM
not sure about what in you terrainBase. Debug.Log w and h in Start function - what they have?
h is 513, w is 513.
and this
Debug.Log(t2ATerrain.terrainData.GetHeights(0,0,w,h));
is also returning this
System.Single[,]
ins$$anonymous$$d of data from in the array.
EDIT: The terrain I'm loading is currently just a default Unity terrain I made with the "GameObject - Terrain" menu option. Default settings, and blank except for a quick heightmap I scribbled on it in the editor to test with. So basically, I am failing to get data from the default terrain here.
Out of curiosity, I tried replacing w and h with numbers like 32, 45, and such other manually typed numbers. I still get blank arrays back. I also tried asking for data from 1,1,25,25 and got a blank back. So it appears that the call to GetHeights is always returning a blank for some reason, even though the terrain clearly exists during runtime.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174129im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity imports heightmaps in kind of upside down order. 0 Answers
Tracking information across terrain 1 Answer
How do you choose the heights of a terrain using code? 1 Answer
Server-side generated heightmap 0 Answers
**Trying to access out-of-bounds terrain height information** When calling TerrainData.GetHeights(); 0 Answers