Digging terrain via script.
Hello guys, Im kinda new in unity3d engine, and im trying to implement some terrain digging via script. I have my terrain which is: Terrain width: 2048 Terrain height: 2048 Heightmap resolution: 513
I am trying to: Get the terrain height in specific point via GetHeights(point.x /4, point.y/4, 10, 10) to heights[,] array I am dividing point x and y coordinates because i am getting the point in a world which is from 0 up to 2048 and im trying to set the height in heightmap which is 0 to 513 (im not sure if im doing it good way, correct me if im wrong). Then i iterate throught the array and subtract 0.1f from each sample. Then im trying to set height via SetHeights(point.x/4, point.y/4, heights);
Im not getting any errors but this one isnt working properly. I cant say if this isnt working at all or it is, but it lowers the terrain in some other point.
Please take a look at it ;)
Here is code sample:
else if (whatHit.collider.gameObject.name == "Terrain" && Input.GetKeyDown(KeyCode.T)){
TerrainData terrain = Terrain.activeTerrain.terrainData;
Vector3 point = whatHit.point;
Debug.Log(point.ToString());
float[,] heights = terrain.GetHeights((int)(point.x/4), (int)(point.y/4), 10, 10);
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
heights[i, j] -= 0.1f;
}
}
terrain.SetHeights((int)(point.x/4), (int)(point.y/4), heights);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612053934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Runtime terrain painting with SetAlphamaps = lag 0 Answers
Updating the terrain collider after importing raw data 4 Answers
Terrain Layers cannot be populated at the same time as the splats 3 Answers
Change Terrain Heightmap Resolution without Resizing Terrain 1 Answer
How to modify a Terrain at runtime and get back to original Terrain on exit? 2 Answers