Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What am I doing wrong with Terrain.SampleHeight() ?

Discussion in 'Editor & General Support' started by jc_lvngstn, Mar 31, 2013.

  1. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Unity 3.5.7f6.

    I placed a sphere on a terrain, using Ctrl-mouse drag, so that the editor places it. The terrain is placed at 0,0,0. The sphere coordinates says that the y value is 525.7758.
    I have a script which reports the height of the terrain at that point as the same amount (525.7758) . Awesome.

    Now, I shift both the sphere AND the terrain 512 units in the X direction -only-. No change in y or z. So technically, the sphere is at the same place on the terrain.

    Y value of the sphere position is still 525.7758. But the sampleheight value returned is 519 something.

    Here the script that reports the terrain height on what is selected, by the way. Drag the terrain to the Terrain inspector value, select your object on the ground, and click Get Height.

    Does anyone see anything I am doing wrong here? Hopefully just changing the X position of a terrain doesn't change the height the engine reports back.

    Code (csharp):
    1. public class GetWorldHeight : EditorWindow {
    2.  
    3.     public Terrain _Terrain;
    4.     public GameObject Selected;
    5.     private float height;
    6.  
    7.     [MenuItem(@"Game/Misc/Terrain Height")]
    8.     public static void ShowWindow()
    9.     {
    10.         var window = GetWindow<GetWorldHeight>("Terrain Height", true);
    11.         window.Show();
    12.     }
    13.  
    14.  
    15.     private void OnGUI()
    16.     {
    17.         if (Selection.activeObject == null)
    18.         {
    19.             EditorGUILayout.LabelField("Select something in the project window.");
    20.             return;
    21.         }
    22.  
    23.         Selected = (GameObject)EditorGUILayout.ObjectField(Selection.activeGameObject, typeof(GameObject));
    24.         _Terrain = (Terrain)EditorGUILayout.ObjectField(_Terrain, typeof(Terrain));
    25.         if (_Terrain == null)
    26.         {
    27.             EditorGUILayout.LabelField("Assign a terrain.");
    28.             return;
    29.         }
    30.  
    31.         if (GUILayout.Button("Get Height"))
    32.         {
    33.             var terrainLocalPosition = Selected.transform.position - _Terrain.transform.position;
    34.             height = _Terrain.SampleHeight(terrainLocalPosition) + _Terrain.transform.position.y;
    35.         }
    36.  
    37.         EditorGUILayout.LabelField("Height: " + height);
    38.     }
    39.  
    40. }
     
  2. tristanrichter

    tristanrichter

    Joined:
    Oct 28, 2013
    Posts:
    15
    I'd like to know the answer to this question, also.