Detect Mob between 5 and 10 blocks away from you
I am making a lightning sword. I want to strike lightning on a mob that is within 10 blocks, but at least 5 blocks away from me. Is there any command that can do that (Multiplayer friendly)?
Best Answer
What you need to do is to chain the execute command, and use the r and rm target selector arguments. The latter denote maximum and minimum radius distance from the execution point, respectively.
First, you have to execute on every player triggering the lightning sword. For this command, I'll assume those players have a triggerLightning score of 1 (How or when you set the score is beyond the scope of this question/answer.)
At the location of this player, we start looking for a (single, c=1) entity between 5 and 10 blocks away (r=10,rm=5).
At the location of the entity, we summon the lightning bolt.
execute @a[score_triggerLightning_min=1] ~ ~ ~ execute @e[r=10,rm=5,c=1] ~ ~ ~ summon LightningBolt
Pictures about "Detect Mob between 5 and 10 blocks away from you"



How do you detect if a player is in a certain area?
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).Do command blocks have a range?
2. Type the CommandHOW Is This 5 Blocks Away From Spawn?
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Tim Gouw, Ketut Subiyanto, Ketut Subiyanto, Antony Trivet
