# How to translate WORLD coordinates to TERRAIN coordinates?

 0 Recently I've begun messing around with altering the terrain in real-time. One of the hiccups I've run into is that world coordinates are not the same as terrain coordinates (obviously). How can I take a position in the world and translate it to the nearest point on the terrain that I can then alter the height of? I hope my question is clear enough. Any advice is appreciated! more ▼ asked Dec 20 '09 at 06:44 PM user-614 (google) 23 ● 1 ● 1 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 First, subtract the terrain gameobject's world position, then divide by your terrain's size (which you can access in terrainData.size). This will give you a position with values somewhere in the range of zero to one. You can then multiply this by your heightmap resolution (using the terrainData heightmapWidth and heightmapHeight variables), and then convert to integers to find the correct heightmap array position to feed into the GetHeights and SetHeights functions. more ▼ answered Dec 20 '09 at 07:15 PM duck ♦♦ 40.9k ● 92 ● 148 ● 415 private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor) { Vector3 vecRet = new Vector3(); Terrain ter = Terrain.activeTerrain; Vector3 terPosition = ter.transform.position; vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z = ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight; return vecRet; } Is this right? or i should change it like this: vecRet.x *= ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; Mar 12 '11 at 12:30 PM Yanger_xy add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 You could also do a terrain.collider.Raycast: ``` RaycastHit intersection = new RaycastHit(); Ray originUp = new Ray(new Vector3(0,0,0), Vector3.up); terrain.collider.Raycast(originUp, out intersection, 100000.0f); if (intersection.collider != null) { ball.transform.position = intersection.point; ball.rigidbody.position = intersection.point; } ``` more ▼ answered Jan 12 '10 at 03:03 PM darkmath 2 ● 1 ● 1 ● 1 That just gives a world position again. Terrain position is the little blue squares when you paint it. Apr 22 '11 at 07:45 PM Owen Reynolds add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 ``````private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor) { Vector3 vecRet = new Vector3(); Terrain ter = Terrain.activeTerrain; Vector3 terPosition = ter.transform.position; vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z = ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight; return vecRet; } `````` Is this right? or i should change it like this: vecRet.x *= ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z *= ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight; more ▼ answered Mar 12 '11 at 12:30 PM Yanger_xy 270 ● 22 ● 25 ● 33 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email: