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!
asked Nov 16 '09 at 02:19 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.
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.
answered Nov 16 '09 at 09:42 PM
Don't know if you're still interested in this, but I've posted some code for this on the forum:-
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.
answered Nov 18 '09 at 04:19 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.
answered Nov 16 '09 at 07:25 PM