Sample the height of the mesh?

Hello all,

I got some sereius problem. I generated a mesh (it’s long and flat) via script (it’s totally random) I want the camera to go on the mesh. So I sampled the vertices of the mesh and gave the coords for the camera but when the mesh go lower, the camera instantly jump under the mesh, because the vertices are far away from each other. Any idea to keep the camera always on the right position?

To sum up:

  • You have created a “custom hightmap-like-terrain-mesh”.
  • You want the camera to move across the surface of your mesh.

If you sampled a vertex position you will always need to add an offset, otherwise the camera will move directly on or in the surface. Add a small Vector.up to your positions.

If that’s not your problem you should overthink your question. “It’s long and flat” and “totally random” in one sentence is a bit weird.

btw. if the mesh isn’t changing each frame you could use a Mesh collider and raycast against it (maybe you already do this. I’m not sure what you mean by sampling vertices).