How to detect if a player is looking at an entity?
Hey I'm trying to use command blocks to detect if an entity is in a players point of view and then effect said player with instant damage or something along the lines of that. So far this is what I have, but it is not working, please help.
/execute @e[name=SlenderMob] ~ ~ ~ /execute @p[rym=1,ry=2] ~ ~ ~
/effect @s instant_damage 1 0 true
If you don't understand the question please explain what you don't understand so I can evaluate.
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!
Items you can change
- 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 usingfacing entity @p (eyes|feet)
. - The last line.
if entity @s[distance=..0.6]
can have its number changed. The lower the number, the closer the player's crosshairs need to be in order to trigger the final command.