- Home /
Basing a grid off the terrain in unity.
Would it be possible to make a grid based off the polygons of the terrain in unity? I want to make a city building game and start with roads being made off a grid, the best way I can think of doing that is basing it off each polygon on the terrain, but don't know how to do this yet. Can it be done and how would you basically go about doing that.
Answer by AlucardJay · Mar 30, 2013 at 04:30 PM
Yes, check all the terrain script on the Unity Wiki, all by Eric5h5, like I have found while using these for research myself. He Is .... Everywhere! (in a good way, I would be lost without his advice as found on all his answers and freely provided code. Thanks again Eric, you are a Legend). You would be wise to read his terrain scripts and learn from them : http://wiki.unity3d.com/index.php/Scripts/Editor
For more stuff you are looking at, watch quill18creates on YouTube : http://www.youtube.com/watch?v=dDqCPMpX1vI
Edit : Here's how to read the heights of the terrainData. Using this script as a reference : http://wiki.unity3d.com/index.php/RaiseHeightmap
#pragma strict
var myTerrain : Terrain;
var myTerrainData : TerrainData;
function Start()
{
if ( !myTerrain )
{
myTerrain = Terrain.activeTerrain; // find the active terrain
}
myTerrainData = myTerrain.terrainData; // store the terrainData
ReadTerrainAndPlaceCube();
}
function ReadTerrainAndPlaceCube()
{
var heights : float[,] = myTerrainData.GetHeights( 0, 0, myTerrainData.heightmapWidth, myTerrainData.heightmapHeight );
for ( var y = 0; y < myTerrainData.heightmapHeight; y ++ )
{
for ( var x = 0; x < myTerrainData.heightmapWidth; x ++ )
{
if ( x % 20 == 0 && y % 20 == 0 ) // place a cube every 20*20 units
{
var actualHeight : float = heights[y,x] * myTerrainData.size.y; // need to multiply the height by the size.y of the terrain
var cube : GameObject = GameObject.CreatePrimitive( PrimitiveType.Cube ); // create a cube
cube.transform.position = new Vector3( x, actualHeight, y ); // position it at the actual height
}
}
}
}
Thanks those tutorials are awesome I'm learning a lot about scripting meshes but the wiki site you listed . The terrain scripts I see don't look like they would show data of terrain objects to base a grid off of. which one were you talking about specifically? Also modifying the 2d Tilemap script seems like a good place to start.
I have edited my answer to show how to read the heights at a position, calculate the actual height based on the terrainData.size , then place a primitive cube at that position. Hope you like it =]
I've tried to create such a cube map for a terrain of 256x256 elements. I got 999+ messages that I have too many rigid objects or colliders. So, how many collider may I put in one scene?