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!

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.

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.

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.