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.

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);