Get height of a Mesh by X and Z coordinates

Hi,

well I create a mesh dynamically and I set its height by code:

``````    private Tile GenerateTile(int x, int z) {

var Pink = seedBeta;
var n = new CoherentNoise.Generation.Fractal.PinkNoise(Pink); //213321

GameObject plane =
(GameObject)Instantiate(terrainPlane, new Vector3(x * planeSize, 0, z * planeSize), Quaternion.identity);
plane.transform.localScale = new Vector3(planeSize * 0.1f, 1, planeSize * 0.1f);
plane.transform.parent = transform;

plane.name = "Chunk_"+x+","+z;

// Get the planes vertices
Mesh mesh = plane.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;

// alter vertex Y position depending on simplex noise)
for (int v = 0; v < vertices.Length; v++) {
// generate the height for current vertex
Vector3 vertexPosition = plane.transform.position + vertices[v] * planeSize / 10f;

float height = n.GetValue(vertexPosition.x * detailScale, vertexPosition.z * detailScale, 0);
// scale it with the heightScale field
vertices[v].y = height * heightScale + baseLine;
}

mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();

Tile tile = new Tile();
tile.gameObject = plane;
tile.tileX = x;
tile.tileZ = z;

return tile;
}
``````

And well I have some problems with the generation, because sometimes when the terrain is generated the player falls through the terrain because it’s very high…

I want to do a function that calculates the Y value only putting the X and Z coordinates… To set the player at the same height as the terrain…

I want to make an equivalent of TerrainData.GetHeights… But I don’t know where I can start… I found before RayCast and I have been doing some tests… But It wasn’t so useful…

So what can I do?

``````RaycastHit hit;