TransWikia.com

How to detect if a player is looking at an entity?

Arqade Asked by HungryBoy02 on January 3, 2021

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.

2 Answers

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.

Answered by Stetson Daisy on January 3, 2021

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 using facing entity @p.
  • 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.

Answered by ExpertCoder14 on January 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP