- Home /
How to get a terrain's maximum height
seems simple, but I can't find anything that returns me the terrain's maximum height. not in http://docs.unity3d.com/Documentation/ScriptReference/Terrain.html, nor in http://docs.unity3d.com/Documentation/ScriptReference/TerrainData.html
I'm not talking about setting it in the editor, I want to get the value in code (to use in calculations)
What do you mean by "maximum height"? The maximum potential height or the actual highest point?
The maximum potential height. yeah, I only realised afterwards that that wasn't very clear.
Answer by AlucardJay · Feb 20, 2013 at 01:32 AM
use the size variable of TerrainData : http://docs.unity3d.com/Documentation/ScriptReference/TerrainData-size.html
var terrainSize : Vector3 = terrain.terrainData.size;
Debug.Log( "terrainSize : " + terrainSize );
Debug.Log( "terrain Max Height : " + terrainSize.y );
Ah ok, I missinterpreted what the size variable was. thanks, I'll try it
Answer by Kiloblargh · Feb 20, 2013 at 12:23 AM
TerrainData.GetHeights can give you all the heights in a 2d array of float values.
One you have that it should be easy to write a for loop that goes through that array and sets maxHeight to the current value if it's larger than maxHeight now.
No, TerrainData.GetHeights returns heights in a 0,1 range. Which means you have to multiply them by the terrain's maximum height to get their real height (which is exactly what I need it for)
Answer by deep-josh · Nov 23, 2017 at 04:12 AM
How about using Bounds:
_terrain = Terrain.activeTerrain;
float max_height = _terrain.terrainData.bounds.max.y;