This is an updated answer for 1.13, using clever modification of the /execute
command. The command starts at the player's position, moves one block towards the target, then moves one block backwards from the direction the player is actually facing. If the two movements cancel each other out and we end up near our starting point, it means that the player is facing the target.
execute
as @a # For each player,
at @s # start at their feet.
anchored eyes # Looking through their eyes,
facing <X Y Z> # face perfectly at the target
anchored feet # (go back to the feet)
positioned ^ ^ ^1 # and move one block forward.
rotated as @s # Face the direction the player
is actually facing,
positioned ^ ^ ^-1 # and move one block back.
if entity @s[distance=..0.6] # Check if we're close to the
player's feet.
run
say I'm facing the target!
In this command, you can:
- Use the
facing
argument to specify the target. You can specify a position by typing the coordinates right in, like so: facing 12 23 34
or you can specify an entity by using facing entity @p (eyes|feet)
.
- change the number in
if entity @s[distance=..0.6]
to adjust the sensitivity. The lower the number, the closer the player's crosshairs need to be in order to trigger the final command.