How to execute a command when an entity is a certain number of blocks away from another entity

How to execute a command when an entity is a certain number of blocks away from another entity - Black and White Scrabble Blocks

I am creating a gun in vanilla Minecraft and I need it so when an armor stand is within a certain amount of blocks away from another entity, it performs a command.

Does anyone know how I can do this?



Best Answer

Try using an execute command to use the range selectors relative to one of the entities:

/execute @e[name=ENTITY1] ~ ~ ~ /execute @e[name=ENTITY2,r=X,rm=X] ~ ~ ~ /COMMAND

Replace name=ENTITY2 with however you're identifying the entity you want to perform the command, name=ENTITY1 with however you're identifying the other entity, COMMAND with whatever command you want to run, and r=X,rm=X with the range you want it to run at (rm is the minimum, so should be smaller than or equal to r).




Pictures about "How to execute a command when an entity is a certain number of blocks away from another entity"

How to execute a command when an entity is a certain number of blocks away from another entity - White and Black Scrabble Blocks
How to execute a command when an entity is a certain number of blocks away from another entity - Big wooden number sign hanging on brick wall of modern building
How to execute a command when an entity is a certain number of blocks away from another entity - Unrecognizable woman holding paw of dog



How do you use the distance command block?

Range 2 is the range a player want for a command block under a floor. Keep in mind that it will produce a globe shaped trigger area (rather than a cube), so a range of 2 makes an approximately 5\xd75\xd75 area and a range of 1 makes a 3\xd73\xd73 area (more like a + pattern with an extra block above and below the center).

Does command block have a range?

The reason I want this in the game is that, say, you have a lost structure in your world with a particular type of block that is not naturrally generated, you could use the command: /locateblock ____. The blank is for the particular block you are looking for.



Minecraft Execute Command [1.19] Tutorial




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Visual Tag Mx, Visual Tag Mx, ready made, Ivan Babydov