ClosestPointOnBounds problem

Ahoy Unityers!

I’m having an extremely annoying problem w/ projectiles and ClosestPointsOnBound.

I’m trying to detonate a missile and find the closest point to the detonation on a nearby object. My code is as follows:

if (hit.rigidbody) {
			
var closestPoint = hit.collider.ClosestPointOnBounds(transform.position);
direction = closestPoint - transform.position;
				
print(hit.name + " " + transform.position + " " + closestPoint + " " + direction.normalized);
				print(transform.position.x + " " + transform.position.y);
				print(closestPoint.x + " " + closestPoint.y);

}

What I’m finding is that transform.position and closestPoint are IDENTICAL. However, the detonations are CLEARLY happening well above the object. Why!!?? :[.

Right now, i’m going to try to work around it, but ClosestPointOnBounds seems REALLY useful and I’d really like to know how to use it properly.

Thanks!

-Sean

Personally, I think of myself as a Unitoid, but no matter :wink:

Collider.ClosestPointOnBounds returns the closest point on the collider’s bounding box, which could be a fair distance from the object’s surface, depending on its shape. If your target object has a MeshCollider, you may be able to use a linecast to get the exact point of contact on the mesh surface. The start and end of the line would be the projectile’s and target’s transform.position values, respectively.