(Java Minecraft 1.14.2) How to use a target selector to determine if the executing position is in an entities hitbox?

I am writing a datapack with an item which functions like a sniper rifle - you can shoot it and a particle beam created by a recursive raycasting function will shoot out and any entities it encounters should die. However, entity detection is a big problem, because I can shoot right through a creeper's head and it doesn't die. You can download the datapack here if you want to test it out. Use /function rituals:give/sniper_rifle to get the item.

I noticed that this is true for almost every mob in the game greater than an endermite or silverfish: if you shoot it through the feet, it dies, but anywhere above that or else than that, it doesn't. Pigs, cows, zombies, evokers, drowned, wither skeletons, anything, only die when shot through the feet. I don't intend for it to kill bosses so they are not a problem, luckily.

I've been detecting mobs by having every iteration of the recursive function run kill @e[distance=..0.1], and it moves ahead by 0.25 blocks each iteration. I have tried to get around this difference in @e detection, mob hitboxes, and visible mob sizes (which for some reason are all different) by using scoreboards to give every mob a value equal to their approximate height, and then changing the distance based on what type of mob it is: kill @e[scores={mobHeights=2},distance=..2], but that is at best buggy and at worst making it so you can shoot next to a mob and kill it.

As you can see in this image, the cursor is well over the pig, yet it insta-dies because of this imprecision. Pig dying from sniper rifle.

If anyone knows a better way to detect mobs a raycasting function is moving through that would be great, because right now it is really frustrating to have to try to manually implement a fix which is really imprecise. Also, if anyone knows why the function only recognizes entities by their feet, that would also help a lot. I know that coordinates are also calculated at an entities feet, so maybe that has something to do with it.


Solution 1:

distance tests if the target coordinates are less than some distance away, not if the nearest part of the mob is under the given distance. I'd suggest using the 3d bounding box selection (x,dx,y,dy,z,dz,), because it selects hitboxes. It would kill any mobs in an area similar to the bounding box of an entity - meaning you are now tracking a point using a box, instead of the ideal tracking a box using a point.
Here's an example command:

kill @e[x=~,y=~-1.5,z=~,dx=0.1,dy=1.6,dz=0.1]

This would still not work, let's say, for an Ender Dragon. Also, you would kill everything on the way, including items.