This site uses strictly necessary cookies. More Information

X- Home /

# Boat physics

Hi.

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(); // The mesh

Vector3 nearestVertex = The nearest vertex on the mesh "mesh" from "point"

How would this be possible?

Thanks in advance,

Andreas.

Simpply just the nearest vertex on the mesh in Vector3 world space.

**Answer** by Benproductions1
·
Apr 10, 2014 at 07:30 AM

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 ;)

Thanks for your answer. But how exactly do I perform a linear seach? I couldn't find any documentation? Or if possible, could you share some sample code? :)

Thanks in advance.

Andreas.

You couldn't do a google search on the term "linear search"? Are you serious?

**Answer** by thomasindustry
·
Apr 09, 2014 at 06:57 PM

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.

Okay, so how do I actually find the nearest mesh vertex?

It would be more efficient to transform the point to local space *once*, rather than the other way around for *every* vertex.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.