How could I compare distances between BaseParts/Attachments to a BasePart and then get the closest to furthest ones?

Recently I have been messing around with a collision dectection system I managed to make and which I am planning on using for a future project of mine, but i’ve ran into a problem.

The way the collision detection system works is: there are Attachments in the middle of the BasePart that is used as a hitbox, with a pattern resembling a wall.

raycastexampleimage0

When a collision happens (through a .Touched event) it Raycasts from all the AttachmentsWorldPosition in the hitbox towards the part, and if one of the Rays hits, then the other Rays are ignored and the hit is registered.

And here is the problem. I know how to get the distance between the Attachments and the hit BasePart, but I’m not really sure how to do the former and then, Raycast from the Attachments that are closer to the BasePart all the way to the most distant ones. Without something like that, the collision detection tends to be weird and innacurate. (i. e. the hitbox gets hit from the left front, but the rays are fired from the front right first instead.)

Any idea of how this could be done?

Pick the desired attachment, and iterate through all the other attachments. While in the iteration, subtract the position vectors and then extract their magnitudes. The one with the most magnitude is the furthest, and the least is closest.

I am on my phone right now, so I cannot give you a sample code. If you want, I can just give you the direct solution.

Yes, i am aware of that way of getting both positions and their magnitudes, but i don’t know how i would compare those magnitudes, and raycast from all the attachments starting from the closest to the furthest…

What do you want to achieve? Ray casting is very inefficient for this.