Search Unity

Question Unity Terrain Display Issue

Discussion in 'World Building' started by Pasko70, Feb 8, 2021.

  1. Pasko70

    Pasko70

    Joined:
    Apr 6, 2020
    Posts:
    5
    Hey guys,
    i have a problem with my terrain. If i set height on my terrain, some times unity flattens all my terrain but just graphicly. My raycasts are still detected on the original terrain height. And also some times it recovers and everything is displayed just fine. It's not possible that my code really flattens all my terrain since these changes are permanent (even if you exit playmode). Any one had some experience with that?


    Edit: Disabling the terrain right after setting the heights and enabling it right after this solves the problem. Still if anyone has some info on it, i would still love to know what is going on.
     
    Last edited: Feb 9, 2021
  2. wyattt_

    wyattt_

    Unity Technologies

    Joined:
    May 9, 2018
    Posts:
    424
    Heya! What does your code look like where you are settings the Terrain heights?
     
  3. Pasko70

    Pasko70

    Joined:
    Apr 6, 2020
    Posts:
    5
    I have a tile system and i set the height on the tile position
    But be carefull it sets the height permanantly. If you exit playmode the changes are still
    in effect.

    Code (CSharp):
    1.     public void LevelTerrain(Vector3 objectPosition, Vector3[,] tilePositions)
    2.     {
    3.         int sizeX = tilePositions.GetLength(0);
    4.         int sizeZ = tilePositions.GetLength(1);
    5.      
    6.         float[,] heights = new float[sizeZ, sizeX];
    7.  
    8.         NodeIndex minIndex = GlobalGridManager.Instance.GetIndexByWorldPosition(tilePositions[0,0]);
    9.  
    10.         float normalizedHeight = objectPosition.y / TerrainSize.y;
    11.      
    12.         for (int x = 0; x < sizeX; x++)
    13.         {
    14.             for (int z = 0; z < sizeZ; z++)
    15.             {
    16.                 //Height Map X and Z swapped
    17.                 heights[z, x] = normalizedHeight;
    18.             }
    19.         }
    20.      
    21.         MainTerrain.terrainData.SetHeights(minIndex.Z, minIndex.X, heights);
    22.     }
     
    Last edited: Feb 23, 2021