This site uses strictly necessary cookies. More Information

X- Home /

# Closest point on mesh / collider

Hi everyone!

I'm stumbling across a small problem, here's hoping someone can help. Basically what I need is the nearest point to an object.

For now, I've been trying to use Collider.ClosestPointOnBounds. It looks perfect! Right? Unfortunately, the results I'm getting from this method are axis-aligned. Whenever the object is rotated (which is just about always), the method returns an invalid point which isn't actually on the surface. So I'll have to use another approach...

Also, it doesn't matter to me whether the nearest point is on the collider or the mesh. I just need a way to know which point on the surface of an object is nearest to another point.

Any help is really appreciated!

**Answer** by duck
·
Nov 16, 2009 at 09:42 PM

Here's a method which will give you the nearest vertex of the gameObject's mesh. I've written this code to be suitable for putting into a MonoBehaviour script, because it uses references to the gameObject's "transform" and "MeshFilter" components.

An alternative would be to make a static "helper tool" version of this function which could accept both a point and a GameObject parameter.

```
public Vector3 NearestVertexTo(Vector3 point)
{
// convert point to local space
point = transform.InverseTransformPoint(point);
```

```
```

```
Mesh mesh = GetComponent<MeshFilter>().mesh;
float minDistanceSqr = Mathf.Infinity;
Vector3 nearestVertex = Vector3.zero;
// scan all vertices to find nearest
foreach (Vector3 vertex in mesh.vertices)
{
Vector3 diff = point-vertex;
float distSqr = diff.sqrMagnitude;
if (distSqr < minDistanceSqr)
{
minDistanceSqr = distSqr;
nearestVertex = vertex;
}
}
// convert nearest vertex back to world space
return transform.TransformPoint(nearestVertex);
```

}

If you really need to find the nearest point within the surface of the nearest triangle of your mesh, this code could serve as a good starting point.

That's an interesting idea, Duck. The problem is that my meshes are rather large, thus this would be rather inefficient. I've already used a different approach, but hopefully your snippet will come in useful to someone else going through the same problem. :-)

So Pgbrandao, what is your solution? Please share, thanks.

Don't underestimate how fast unity's code executes (unless you're targeting the iPhone!). There are ways this could be optimised too. You could spread the process out over a number of frames, only processing a handful of vertices per frame. Alternatively, if the point being tested is likely to move along a path at a reasonable speed, you could make use of the fact that result each frame will most likely either be the same vertex as found last frame, or a neighbour of the vertex found last frame, by using a lookup table of each vertex's neigbours.

I'm really curious about what your solution was. Right now, Duck's solution is my only option, and like you I have a large mesh, so the efficiency thing does concern me. NOTE: I do have one way that I have slimmed down the searching process. What I do is, based on the world coordinate of the point that I am comparing with the mesh, I can slim down the vertex search to a specific series of indices in the vertex array. That cuts down the search from an average of 20k verts to 2k.

I believe the fastest way to do this would be through **shaders**... But I haven't done it yet. In any case, I'd love to read more about @pgbrandao's solution. And, @$$anonymous$$ilometers you talk about "slimming down the vertex search to a *specific* series of indices, based on the world coordinate" but that doesn't say much about the solution **as well**, dude - the irony here is that you did ask the other dude for an explanation but you didn't really gave yours.

Just out of interest - the nearest point on the mesh rather than the nearest vertex can be found using this method: http://answers.unity3d.com/questions/424974/nearest-point-on-mesh.html

**Answer** by andeeee
·
Nov 18, 2009 at 04:19 PM

Don't know if you're still interested in this, but I've posted some code for this on the forum:-

http://forum.unity3d.com/viewtopic.php?t=36772

It's a bit like the code Duck suggested, but it also implements the nearest point on triangle part and uses a spatial data structure to store the vertices so as to reduce the overhead of the search.

**Answer** by VoxelBoy
·
Nov 16, 2009 at 07:25 PM

As a quick workaround, you could try the following in whatever script you're calculating the point from: -Un-rotate the object with the collider so it's rotation = Quaternion.identity. Keep the previous rotation in a temp. variable. -Use the Collider.ClosestPointOnBounds method to calculate the Point -Rotate the object back to it's original rotation. -Rotate the point along the object's position with the same rotation you applied to the object.

This should, in theory, result in a point that's on the object/collider's surface even when it's rotated.

False. Apart from the fact, that the operations are listed in a wrong order, this would only work if the object/collider was a right cuboid (a rectangular box), because only then the bounds would align with object's/collider's faces.

### 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.

### Follow this Question

### Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I break apart an object? 2 Answers

How does Unity3d uses nvidia's PHYSX engine? 0 Answers

Finding the axis of a curving pipe 0 Answers

Manual collision check 2 Answers