# How to get height of mesh at pos (x, z) if the mesh does not have a collider?

I’m beginner and confused. Here is my situation:

I have a cube above a noisy plane (plane represents water). I’d like to get the height of the plane (“wave height”) at cube’s position. What might be the way to do this? The plane does not have a mesh collider, since the cube is meant to float inside of it.

Also, I am manipulating the plane’s actual vertices with noise, and I don’t want to use a formula or a shader for my purposes. In other words, instead of “cheating” and simply feeding Cube the “wave height” via formulatic means, I’m really wanting to find the Plane’s actual vertices height at cube’s position. But, I don’t understand how to. Any ideas?

Thanks!

You assume there will be a vertice at the cube position, it might not be, you might be above a triangle instead. There is many solutions that are more or less dumb.

The “dumb” solution is:

• assuming your plane is always axis aligned (here it’s xz)
• compute the delta with the plane origin and the cube origin
• use the delta to compute the cube position relative to the plane
• iterate all vertex to find the closest, find the triangles associated, easier if your vertex are well laid out and at regular distance of each other, to which you can simply do div and a modulo to find the “corner vertex” of a virtual tile.
• compute the lerp from the vertices to find the height

The smart solution is:

• don’t be dumb and pass the right formula, that’s no cheating
• if you don’t want that, use a freaking collider as a trigger, ie when overlapping the frakking raycast and use the command to find closest triangle

Other solutions is to spatially partition reference to the vertex and triangles in a grid structure, find the position of the of the cube within that partition, do the dumb solution in xz to select the right vertices

2 Likes

You can also simply convert the global coordinates of the cube to local coordinates of the Plane. Since the vertices of the mesh are always in the local coordinates of the object, you can iterate through all the vertices and find the closest ones located at the local coordinates of the cube (which were calculated earlier). Then, find the average position from the set of closest vertices, convert it to global coordinates, and set the cube’s position there.

2 Likes

The plane having a mesh collider should not stop the cube “floating” in it I guess you could define sea level as 0, ray cast against the wave at the desired coordinates and have your height?

ps: you are at least an “advanced beginner” based on what you seem to be doing or you might bite off …

1 Like