- Home /
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?
`http://kepfeltoltes.hu/110608/mesh_www.kepfeltoltes.hu_.jpg`
i dont quite get your question, what exactly is the camera supposed to do?
Just always go forward on the top of the mesh. It's like a plane mesh, with different heights.
Answer by Bunny83 · Jun 08, 2011 at 04:07 PM
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).
"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."
So it's always flat like a terrain, the length is random generated, the width is always 50, and on this mesh the heights are random generated.
I already tried $$anonymous$$esh collider, but there was always some error because I can't predefine the mesh for collider, because it's generated at runtime. So I used that Shared$$anonymous$$esh thing, but I it doesn't looked like it's working.
And the first solution what you said it's not good because the degree of the up and down is not always the same. The second probame, if I go lower, the vector up help to stay on the top of the mesh, but when I go up it will go higher then normally. Third problem is what I can't explain but its really big :DDD
If a mesh has different heights how can it be flat? Anyway, I still don't get your problem...
You said you want to keep your camera on the "right position". What do you consider right? The camera is moving so it's not a position, it's a path.
$$anonymous$$aybe you can take a screenshot of your mesh? You can edit your question at any time.
Ok, that's how i thought that it would look like. I don't see your problem by adding Vector.up to your position. The camera will always have this distance to the surface. You have to sample the position right below the camera of course.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101456im_/https://answers.unity.com/themes/thub/images/avi.jpg)