Find closest object to a ray?

Hi there,

I’m trying to implement an aim assistant that puts the aimpoint (where the user is pointing at) over an object when the user misses by a small amount.

The easy part is when the ray hits: to find the object that is aimed at, I’m using a raycast. That works fine.

However, there are times when the ray doesn’t hit anything, and I would then like to be able to snap the aimpoint to the closest object it passes (if it is within reasonable limits).
For this, I would need to find the object (I’m using a virtual sphere instead of a collider, as the math is trivial) comes closest to the ray. Now, of course I can brute-force this (it might even be a valid approach since there are only a couple hundred objects that can serve as valid targets in each scene), but I was wondering if there is a more elegant approach.

Any hint would be appreciated.

Thanks,
-ch

@csofranz

Why not use SphereCast (maybe I’m missing something in your description…)?

1 Like

Ah. Mainly because I’m an idiot. Thank you so much, never thought of that.

-ch

2 Likes