Collider ClosestPoint and ClosestPointOnBounds are just returning the point I pass them?

Hello! I’m having some issues with basic collider functions being used in (I think) a very basic way. I’ve got two objects:

  1. A rocket, with a simple box collider covering its bounds. It also has a Kinematic rigidbody, and IsTrigger is checked. It is tall and narrow.

  2. A small planet, that is one mesh, with a mesh collider. No rigidbody, not marked Trigger (but it still trips the OnTriggerEnter function just fine)

OnTriggerEnter is working perfectly! No matter how intricate my planet mesh, the function is called on the Rocket’s script only & exactly when the two colliders start to intersect.

Now I’d like to detect, roughy, where this collision happens. So I’m using this code, which seems pretty simple and straightforward:

class Rocket
{
    void OnTriggerEnter(Collider planetCollider)
    {
        Vector3 rocketCenter = this.transform.position; //Center of the rocket
        Vector3 closest = planetCollider.ClosestPoint(rocketCenter);
        Vector3 closestBounds = planetCollider.ClosestPointOnBounds(rocketCenter);
    }
}

But both closest and closestBounds are just giving me rocketCenter! Even when the rocketCenter is clearly NOT near the planet. Even if I make the rocket extra tall, so that the center is even farther from the planet mesh, it’s still doing this!

I must be missing something fundamental. Can anyone help figure out what’s going wrong with these functions?

hi, Did you got the Answer?

Hi, I ran into the same problem and found your question. I guess that the problem has been resolved since a while, but I try to answer for future reference.

It may be that your mesh collider is not convex. The reason is that Collider.ClosestPoint is based on Physics.ClosestPoint that cannot handle non-convex meshes, because they don"t have any thickness and therefore volume. This specificity is explicited in Physics.ClosestPoint documentation but not in the Collider one.

If you can, just enable the convec property of your mesh, you can even do it via script with MeshCollider.convex. It should work afterward.