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.

• Would it work to just put an invisible enderman at that position? Otherwise you need something called "raycasting", which either needs a lot of Math or a lot of commands. Oct 15, 2017 at 1:09
• the enderman thing might work but how might i detect who looked at the enderman? Oct 15, 2017 at 1:10
• And how would i detect if the enderman got agrovatted? Oct 15, 2017 at 2:43
• I'm not sure. There's probably no elegant solution to this. But raycasting also can't be called an "elegant solution". If you just google "Minecraft raycasting", there are many different versions. Does that do what you want? Oct 15, 2017 at 4:02
• Unfortunately no, this did not help. Oct 20, 2017 at 0:52

2 Answers

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.

I'm not a pro at these things, but you might want to get rid of the 2nd and 3rd slashes, and put[name=SlenderMob]right next to @e. If that doesn't work I can't help you.

• Wait is this command for 1.13? Aug 3, 2018 at 13:57
• This question was asked in October 2017 so it most definitely was for 1.12. Aug 3, 2018 at 14:19