How to check if entities have the same position
Im making a Snake Game using Armor Stands on the positions of table game and for the body of the snake. I want to check when the the Armor Stand {Tags:["head"]}
of the snake has the same position of an Armor Stand {Tags:["food"]}
of a food.
(1.16 Version)
(The others Armor Stands are invisible)
I already know two ways to do that.
1. Testfor:
The testfor of 1.12 can be rewrited as, for 1.16:
execute at @e[tag=food] if @e[tag=head,distance=..0.5] run tellraw @a "Same Position Check"
2. Execute Store:
execute store result score @e[tag=head] X run data get entity @e[tag=head] Pos[0]; for X coordinate
execute store result score @e[tag=head] Y run data get entity @e[tag=head] Pos[1]; for Y coordinate
execute store result score @e[tag=head] Z run data get entity @e[tag=head] Pos[2]; for Z coordinate
execute store result score @e[tag=food] X run data get entity @e[tag=food] Pos[0]; for X coordinate
execute store result score @e[tag=food] Y run data get entity @e[tag=food] Pos[1]; for Y coordinate
execute store result score @e[tag=food] Z run data get entity @e[tag=food] Pos[2]; for Z coordinate
And then, test the scoreboards scores of the coordinates X,Y and Z:
execute if score @e[tag=head] X = @e[tag=food] X if score @e[tag=head] Y = @e[tag=food] Y if score @e[tag=head] Z = @e[tag=food] Z run tellraw @a "Same Position Check"
How
Check if the other armor stand is within distance 0.5.
Selector
@e[name=“Example”,distance=..0.5]
Testfor
1.13+
/execute if @e[name=“Example”,distance=..0.5]
1.12-
/testfor @e[name=“Example”,distance=..0.5]