- Home /
Get exact terrain height
I need to place a small rectangle on the terrain at runtime (cursor in a grid-based game). The terrain has hills n whatnot, so I need to know the height of the terrain at all four corners of the rectangle, otherwise its mesh gets hidden under the terrain.
I tried using Terrain.SampleHeight() to do this, but it's very inaccurate and about half of the time, the reported height is lower then the actual terrain height. Do I need to raycast or is there a simpler way?
Answer by fred_gds · Aug 31, 2013 at 01:17 PM
Well why don't you raycast all 4 corners?
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Then you get the exact point where the raycast hits the collider (RaycastHit.point.y I think)
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
That's what I suggested in my question as well. Still wondering if there's a better approach to this. Will probably use a projector.
Your answer
Follow this Question
Related Questions
Editing Terrain Topology at Runtime 0 Answers
Terrain height raycasting? 1 Answer
Unity 5 Terrain glitch 0 Answers
Get Height of Terrain in Script 1 Answer
How do I get the y coordinate of a terrain at any time? 1 Answer