- Home /
Positioning Unity Terrain
I'm trying to add terrain to a unity game with code, then move it's center to (0,0).
Eventually I realized I can create it using:
GameObject map = Terrain.CreateTerrainGameObject(new TerrainData());
The problem is positioning it. I can change where it is, but after a few hours of looking, I still can't find the width/height/depth of the terrain. My best guess is:
map.transform.Translate(new Vector3(map.GetComponent<Mesh>().heightmapWidth/2, 0, map.GetComponent<TerrainData>().heightmapWidth/2));
But that obviously doesn't work right.
Answer by Owen-Reynolds · Feb 11, 2012 at 04:04 PM
If you look up the Terrain component, there's nothing about the size. But it leads you to TerrainData, which has size
in world units. Terrain objects are built with (0,0) at the near-left corner. To put the center at (0,0) the corner will have to be pretty far negative. Translate slides you, better to just put it where you want. So:
Vector3 TS = map.GetComponent<Terrain>().terrainData.size;
map.transform.position = new Vector3(-TS.x/2, 0, -TS.z/2);
Your answer
Follow this Question
Related Questions
How to use the Terrain Transparency? 1 Answer
(script) how to get terrain to turn into blocks 0 Answers
I have a problem while making terrain 1 Answer
Terrain hill generating by code 2 Answers
Unity3D Terrain Help [3DS Max] 1 Answer