4

Is there a command that would kill the entity that the player is looking at?

1 Answer 1

5

This is another question involving raycasting.

To perform raycasting you need a datapack. For how to set up a Minecraft Datapack, look for tutorials made by Cloudwolf, Legitimoose and the likes.

In the datapack functions folder, you want a function called raycast.mcfunction

In the function,

tag @s add this
execute if entity @e[tag=!this,distance=0..0.99] run kill @e[sort=nearest,limit=1,tag=!this]
tag @s remove this
execute if block ~ ~ ~ air positioned ^ ^ ^0.2 run function namespace:raycast
execute if block ~ ~ ~ cave_air positioned ^ ^ ^0.2 run function namespace:raycast

After you put the above code into your function, you simply need to call the function from your tick function or in the game using a repeating command block. Something like this would be useful : execute as PlayerName at @s positioned ~ ~1.6 ~ run function namespace:raycast

Go into the game and slash reload. To this point you should notice that whatever your crosshair points at will die immediately. How it works is that you are creating a recursive function (a function that calls itself) that runs itself across the 3D MInecraft World and everytime the function catches an entity in its way within a 0.99 meter proximity you will kill that entity. The player itself however is immune from this effect because of the tagging command which allows the function to always exclude the command initator while trying to kill the closest entity.

Another thing is that there are countless entities in Minecraft. The popular belief is that only Mobs like Zombies and Creepers are entities which is very wrong. Arrows, armor stands, area effect clouds, markers, minecarts, paintings etc. actually makes up for a larger percentage of the total entity count in Minecraft. If you don't wish to gaze at your painting and watch as it disappears, you should define an entity group called #exclude which you can add to the kill selector to neglect unimportant entities.

Here is a .json file I use for #exclude.

{
    "values":[
        "experience_orb",
        "arrow",
        "spectral_arrow",
        "painting",
        "snowball",
        "egg",
        "trident",
        "armor_stand",
        "area_effect_cloud",
        "item",
        "marker",
        "minecart",
        "chest_minecart",
        "command_block_minecart",
        "furnace_minecart",
        "hopper_minecart",
        "spawner_minecart",
        "tnt_minecart",
        "dragon_fireball",
        "fireball",
        "end_crystal",
        "ender_pearl",
        "evoker_fangs",
        "experience_bottle",
        "eye_of_ender",
        "falling_block",
        "firework_rocket",
        "glow_item_frame",
        "item_frame",
        "leash_knot",
        "lightning_bolt",
        "llama_spit",
        "potion",
        "shulker_bullet",
        "small_fireball",
        "tnt",
        "axolotl",
        "salmon",
        "tropical_fish",
        "cod",
        "bat",
        "item"
    ]
}

Hope this helps. I can provide you with a sample datapack in case something goes wrong.

6
  • hmm can I do this in one command?
    – OctonalXX
    Jan 21 at 9:20
  • 1
    @OctonalXX As hfanatic said, you can't do this with commands alone, you need a data pack.
    – Diehe
    Jan 21 at 14:40
  • why's it not possible with commands alone? Jan 21 at 15:42
  • Datapacks are indeed commands, and commands are considered vanilla. I think what you want is to shorten the command as much as possible but it is sadly unachievable. As far as the current minecraft version is concerned, the raycasting command has not been coded into the game. So for now you would have to stick with creating the datapack yourself.
    – hfanatic
    Jan 21 at 16:04
  • I mean not doing a loop to kill them, executing the command once to temporary kill an entitie
    – OctonalXX
    Jan 25 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .