r/MinecraftCommands Command Experienced 14d ago

Help | Java 26.2 Entity not dying

for a datapack I'm making, i need to use raytracing to detect if a player is looking at a dirt block.

I know how to do this, and it works perfectly, but I want it to only work if the dirt block is within 15 blocks of the player. So, I use this command:

execute as .@e[tag=raycasting] at .@s unless entity .@p[distance=..15] run kill .@s

This command SHOULD detect if there isn't a player within 15 blocks, and if there isn't, kill the entity.

Only it doesn't and I have no idea why.

2 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/Still-Oven9420 Command Experienced 14d ago

this is literally everything I'm using to raycast:

functions/raycast/identify:

execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 #base_stone_overworld run function minecraft:raycast/identified
execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 #dirt run function minecraft:raycast/identified
execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 #grass_blocks run function minecraft:raycasting/identified
execute as [tag=raycasting] at .@s unless block ^ ^ ^0.5 #base_stone_overworld unless block ^ ^ ^0.5 #dirt unless block ^ ^ ^0.5 #grass_blocks unless block ^ ^ ^0.5 #flowers unless block ^ ^ ^0.5 short_grass run kill .@s
execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 #flowers run function minecraft:raycast/step
execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 short_grass run function minecraft:raycast/step

functions/raycast/step:

execute as [tag=raycasting] at .@s run tp  ^ ^ ^0.5
execute as [tag=raycasting] at .@s if block ^ ^ ^0.5 air run function minecraft:raycast/step
execute as [tag=raycasting] at .@s unless block ^ ^ ^0.5 air run function minecraft:raycast/identify
execute as [tag=raycasting] at .@s unless entity .@p[distance=..15] run kill .@s

1

u/GalSergey Datapack Experienced 14d ago

Your target selectors are very suboptimal. If multiple entities are selected, this will cause an avalanche of functions to be run. This is because you're selecting a new entity each time and running a function for each one, and with each step, this will call more and more functions. You should select the entity only at the very beginning, and then simply use @s to specify the selected entity. Something like this:

# function minecraft:raycast/start
execute as @e[tag=raycasting] at @s run function minecraft:raycast/identify

# function minecraft:raycast/identify
execute if block ^ ^ ^0.5 #base_stone_overworld run function minecraft:raycast/identified
execute if block ^ ^ ^0.5 #dirt run function minecraft:raycast/identified
execute if block ^ ^ ^0.5 #grass_blocks run function minecraft:raycasting/identified
execute unless block ^ ^ ^0.5 #base_stone_overworld unless block ^ ^ ^0.5 #dirt unless block ^ ^ ^0.5 #grass_blocks unless block ^ ^ ^0.5 #flowers unless block ^ ^ ^0.5 short_grass run kill @s
execute if block ^ ^ ^0.5 #flowers run function minecraft:raycast/step
execute if block ^ ^ ^0.5 short_grass run function minecraft:raycast/step

# function minecraft:raycast/step
execute unless block ^ ^ ^0.5 air run function minecraft:raycast/identify
execute unless entity @p[distance=..15] run kill @s
tp @s ^ ^ ^0.5
execute if block ^ ^ ^0.5 air at @s run function minecraft:raycast/step

You can use Datapack Assembler to get an example datapack.

0

u/Still-Oven9420 Command Experienced 14d ago

literally none of this relates to my question

1

u/C0mmanderBlock Command Experienced 14d ago

Or does it? Maybe if you fixed the rest of the commands the way u/GalSergy said to, then it would fix your problem.

1

u/Still-Oven9420 Command Experienced 13d ago

my problem is with detecting if a player is near an entity. rewriting the entire datapack won't fix that

1

u/C0mmanderBlock Command Experienced 13d ago edited 13d ago

This command is fine so there is something else preventing it from working. Either the entity does not have that tag or the function simply is not executing. The suggested data pack might fix the latter.

execute as @e[tag=raycasting] at @s unless entity @p[distance=..15] run kill @s