- Home /
Edit Terrain at runtime
Hello. So i have a raycast being drawn from my camera and hitting the terrain. How do I go about making a small hole in the terrain, where ever the ray hits? Kind of like in the editor, pressing Shift and LClick to make the area go down.
Answer by Salmjak · Mar 14, 2016 at 08:08 AM
@kevinrocks_786 You should take a look at terrainData. Specifically GetHeights() and SetHeights().
Basically what you do is editing the height map and applying it to the terrain. This can be a rather slow process and it's recommended that you use this function instead (a rather new function).
Note that the heightmap positions are not the same as world-positions! So you will have to convert your raycast hit.point to terrain-coordinates. This post shows you how to do it.
So how would you use all this for your purpose? First you convert your hit.point into terrain-coordinates. Then you use terrainData.GetHeights() to retrieve an array of float-values from the height map (I would retrieve 9 values, which would be the grid area around your hit.point). Now you loop through this array and subtract the values. Then you use terrainData.SetHeights/SetHeightsDelayLOD to apply the new heights.