Boat physics


I’m trying to make a boat physics script… To do so, I have to find the nearest vertex on a mesh.

So how can I find the nearest vertex on a mesh?

For exampel:

Vector3 point = (1,33,7); // The Vector3 from where it should find the nearest vertex on
the mesh “mesh”.

MeshFilter mesh = OceanGameObject.GetComponent< MeshFilter >(); // The mesh

Vector3 nearestVertex = The nearest vertex on the mesh “mesh” from “point”

How would this be possible?

Thanks in advance,


the vertices property on a mesh will give you the local positions of all the vertices of the mesh. You can then use the Transform.TransformPoint method to convert these positions to world space for comparisons with your reference point.

Perform a linear search through all the vertices in your mesh, finding the vertex with the least distance to your point in local space. To transform the point from world space to local space, use Transform.InverseTransformPoint.

To speed it up you could use some sort of tree structure instead of a linear search and find the nearest vertex that way, however that might get a little complicated :wink: