Search Unity

Terraform terrain on impact

Discussion in 'Scripting' started by AlphaVexen, Aug 20, 2019.

  1. AlphaVexen

    AlphaVexen

    Joined:
    Apr 19, 2019
    Posts:
    5
    Anyone know of a way to lets say lower or raise terrain when an object hits it? Example, a large rock falling and creating a dent in the terrain?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You could try to use SetPixel() on terrain heightmap. I think it should work.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    There are two problems here, so break them apart and tackle them one at a time.

    Problem 1: converting the position (and size) of the rock in world space coordinates into a heightmap address on the terrain. This has to take into account the position of the Terrain object, the dimensions of the terrain, etc. It's quite a few steps but Unity's API makes it very easy. I have some examples of interoperating between world space and terrain heightmap space in my Makegeo project, which contains a TerrainRampMaker sub-project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    Problem 2 is actually deciding how much to dent the rock, and iterating the heightmap array information from the TerrainData that the Terrain uses, and then writing it back. The above example also covers that step as it modifies the ground to create a smooth Terrain ramp.