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.
public class GetWorldHeight : EditorWindow {
public Terrain _Terrain;
public GameObject Selected;
private float height;
[MenuItem(@"Game/Misc/Terrain Height")]
public static void ShowWindow()
{
var window = GetWindow<GetWorldHeight>("Terrain Height", true);
window.Show();
}
private void OnGUI()
{
if (Selection.activeObject == null)
{
EditorGUILayout.LabelField("Select something in the project window.");
return;
}
Selected = (GameObject)EditorGUILayout.ObjectField(Selection.activeGameObject, typeof(GameObject));
_Terrain = (Terrain)EditorGUILayout.ObjectField(_Terrain, typeof(Terrain));
if (_Terrain == null)
{
EditorGUILayout.LabelField("Assign a terrain.");
return;
}
if (GUILayout.Button("Get Height"))
{
var terrainLocalPosition = Selected.transform.position - _Terrain.transform.position;
height = _Terrain.SampleHeight(terrainLocalPosition) + _Terrain.transform.position.y;
}
EditorGUILayout.LabelField("Height: " + height);
}
}