- Home /
Collider.ClosestPoint returns inconsistent values for Mesh Collider
I use Collider.ClosestPoint to constrain a small object from exiting the collider of a main object. Example - small object is a plate and a main object is a table. The user is able to drag the plate on top of the table and it works really good for rectangle tables where I have a BoxCollider. Collider.ClosestPoint always returns the closest point on the collider (table), and the user can't drag the plate out of the table surface. For oval tables where I am using a convex Mesh Collider the movement is not smooth. As I am dragging the plate on top of the table, it jumps often to the centre of the collider. It happens both when I am dragging the plate inside the table surface as well as when I try to drag it out of the table surface.
Code example:
Vector3 obtainedPoint = [point in the world where small object wants to be moved]
Vector3 constrainedPoint = collider.ClosestPoint (obtainedPoint) // constrain it to the collider
constrainedPoint.y = obtainedPoint.y; // here I constrain to the surface of the table, the dragging only happens in xz plane
// this works for BoxCollider perfectly, but with convex Mesh Collider I get weird jumps towards the center
Your answer
Follow this Question
Related Questions
Issues with collisions 1 Answer
Editing Mesh Colliders? Creating a usable pot/cup 1 Answer
Colliders on this spider 1 Answer
My Colliders Sometimes Works and Sometimes Doesn't 0 Answers
Player collides with open doorway? 1 Answer