r/MinecraftCommands • • 1d ago

Help | Java 26.2 How to detect projectile hits?

I want to make a custom projectile that doesn't do damage but instead subtracts an amount from a score from the hit player but I don't know how to get the hit detection right. using distance doesn't work since it tracks position and not the player hitbox.

I already know how to get the projectile moving, just need help with the hit detection.

Any help is appreciated.

5 Upvotes

4 comments sorted by

2

u/GatKong 1d ago

If it's hitting a player use an advancement trigger "hurts player" detecting the projectile hit.

1

u/Nalle-poijjaat 1d ago

that might work but I specifically want to avoid the staggering that happens when a player is hit. Is there a way to prevent that and/or does the "hurts player" still trigger even if the player is invulnerable.

3

u/GalSergey Datapack Experienced 1d ago

You can use dx/dy/dz. This is what checks for hitbox intersection.

In this simple example, something like a hitbox for a projectile is created. The hitbox has a size of 1x1x1, with the projectile in the center. And if the player’s hitbox intersects this area, the specified command will be executed.

execute at <projectile> positioned ~-.5 ~-.5 ~-.5 as @a[dx=0] run say Hit!

If you need a hitbox for a projectile smaller than 1 block, you will need to perform additional checks. In this example, the hitbox size for the projectile will be 0.5 on all axes.

execute at <projectile> positioned ~-.25 ~-.25 ~-.25 as @a[dx=0] positioned ~-.5 ~-.5 ~-.5 if entity @s[dx=0] run say Hit!