- Home /
SampleHeight vs GetHeight?
Why terrain.SampleHeight and terrain.terrainData.GetHeight returning diferent results? Hi i work with terrain (placing Object on it) and when i work with height of terrain a discover that these two method returnig diferent result (and i cant find out why).
float height = terrain.SampleHeight(new Vector3((int)position.x, 0, (int)position.y));
float height2 = terrain.terrainData.GetHeight((int)position.x,(int)position.y);
Debug.Log(height.ToString() + ":" + height2.ToString());
(retype to int just for purpose of test) and result are guite interesting Sometimes is result almost same like (89.60941:85.10804), but i found few position which are totaly diferent like (0:80.23256) It seems that Sampleheight is more acurate, but why?
Answer by tanoshimi · Jul 20, 2014 at 09:17 AM
terrainData.GetHeight expects an int x and y coordinate expressed relative to the size of the terrain. So, if your terrain is 2048x2048, valid x and y would range from 0-2048, and it returns the height of the terrain from the heightmap at that point.
terrain.SampleHeight expects a Vector3 position in worldspace, and returns the interpolated height of the terrain at that point.
So they measure totally different things - if you were getting similar results from a given input that's coincidence.
But when i place Terrain to 0,0,0, that SampleHeight(new Vector(){10,...,10} and getheight(10,10) should return same Value or not? If of course terrain is big enough (more than 10x10) Or that interpolated value is that much diference?
From memory, I think terrain heightmaps have (0,0) at the top left corner not the bottom left. So I would expect SampleHeight(new Vector(){10,...,10} and GetHeight(Terrain.height-10,10) to be (approximately) the same.
Ok thanks, i will test it and post result $$anonymous$$aybe last question: GetHeight is faster than SampleHeight? (reagin from heightmap - picture vs calculating some positions)
Yes, GetHeight should be faster, because it's basically just a lookup into an array.