- Home /
Find closest point of one object
That is basically it. I need to find the closest point in one object's mesh or collider, to another one (or: the closest point of a mesh to a second point in a certain radius).
I have tried some approaches but fail. I tried creating a "trigger" CirlceCollider, then get the collisions with other colliders, but that only gives me the points were the colliders intersect. There may be simple way to do this but I've search and think for two days and I just can't figure this one out. Any help would be very appreciated.
EDIT: Re-uploaded the image.
If your objects are fairly regular like your picture then the midpoint between points of intersection might be a good approximation. You might cast a ray at that point and find where it collides with you object.
@jonSG: Yes, that was my reasoning too, but it I was hopping maybe there was a better approach... Thanks anyway.
If this is something you are going to be doing over and over, your mesh is pretty detailed, and if you can live with an approximation, you could take the vertices of the mesh and convert them to world space points. Then you could just run through the array and compare your position to these points.
Expanding on @robertbu's comment, if you want a perfect solution, find the closest point between the closest points of every line-segment that makes up both meshes. Would be quite expensive though.
Answer by Lofar42 · Nov 30, 2015 at 06:40 PM
Hey, I realise this is over a year old now so I don't expect I'm adding anything of use really, but I was looking for an answer to the same question and eventually came across this:
http://docs.unity3d.com/ScriptReference/Collider.ClosestPointOnBounds.html
This should give you a point on the bounding box, which you could then raycast towards to find the actual hit position. Still wouldn't be EXACTLY accurate but would be a closer approximation I think.
Answer by rathoddhaval · Apr 17, 2014 at 04:04 AM
Answer Is In Your Question Where U draw Circle. In Unity Using Physics.SphereCastAll Returns All The RaycastHit In Array From(RaycastHit[] array) That You Can Find Min Distance Hit Point.
Like Example
RaycastHit GetMinDistancePoint() { RaycastHit[] points = Physics.SphereCastAll(transform.position, 10, transform.forward); float minDist = 100; RaycastHit selectionPoint; foreach (RaycastHit hit in points) { if (hit.distance < minDist) { minDist = hit.distance; selectionPoint = hit; } } return selectionPoint; }
format your code
read the question, that's not what it's asking
Answer by matbb · May 03, 2020 at 10:43 AM
Had the same question years after the question, came here, my updated answers (2020) are below.
Use "Physics.ClosestPoint" (gives closest point on a colider as asked in the question) or in some cases "Physics.ComputePenetration" and find the location from the penetration direction, while making the first object a small sphere.
Your answer
Follow this Question
Related Questions
Collider added far from object 1 Answer
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
How do I display the objects collider box in a running scene 2 Answers
Why can't compiler find Physics.ClosestPoint or Collider.ClosestPoint? 1 Answer
Closest object from player relative to swipe direction 0 Answers