0

I need to edit terrain in real-time when I click on terrain at that position to raise that part. the code work just fine but the problem is when I change the terrain position it doesn't, work anymore. I get this error :

ArgumentException: Trying to access out-of-bounds terrain height information.

this is my code

public void RaiseTerrain(Terrain terrain, Vector3 location, float effectIncrement)
    {

        heightmapPos.x = (rayHitPoint.x / terrainSize.x) * (float)(heightmapWidth);
        heightmapPos.z = (rayHitPoint.z / terrainSize.z) * (float)(heightmapHeight);
        Vector3 tempCoord = (new Vector3(heightmapPos.x, heightmapPos.y, heightmapPos.z));

        int terX = (int)(int)tempCoord.x ;
        int terZ = (int)(int)tempCoord.z;

        float[,] heights = terrain.terrainData.GetHeights(terX, terZ, 2,2);

        for (int xx = 0; xx < 2; xx++)
        {
            for (int yy = 0; yy < 2; yy++)
            {

                heights[xx, yy] += (effectIncrement * Time.smoothDeltaTime);


            }
        }
      
       terrain.terrainData.SetHeights(terX, terZ, heights);
    }

if I tried to remove the terrain position from the position like so :

Vector3 tempCoord = (new Vector3(heightmapPos.x, heightmapPos.y, heightmapPos.z) - terrain.GetPosition());

but the terrain raise in an other position

Dev
  • 1
  • 4

0 Answers0