TransWikia.com

How can I make an execute command there is a player looking at another player?

Arqade Asked by David Bull Haugsøen on February 11, 2021

I want to make an execute command execute if any player is looking at any other player. I haven’t been able to find anything about this online, so I am wondering if it is possible at all. I’ve tried some different commands to do this, but I accidentally removed them so I don’t have the commands. They didn’t work so I guess that doesn’t matter.

I’m playing on the 1.15.2 Java edition

One Answer

If you want to be exact, you need to use raycasting. But there is a simpler way to approximate it: Summon a dummy entity at the player that is rotated towards the target, then check whether the dummy entity's head rotation is similar to the player's head rotation.

A problem with this is that the X portion of the player's head rotation ranges from -360 to 360, not just from -180 to 180, and that a rotation of -179° and 179° would not be considered close to each other. That can both be solved by simply checking for a rotation 360° higher and 360° lower as well.

Here is an example function implementing this. It only works for the closest player to where the function is executed and assumes that only one zombie exists in the world. It can be adjusted to more players and more targets, but that requires a more complex setup.

# Place a dummy armour at the player, facing the zombie.
summon armor_stand ~ ~ ~ {Tags:["rotation_helper"],Marker:1,NoAI:1,NoGravity:1,Invisible:1,Small:1,NoBaseplate:1,Invulnerable:1,Silent:1,DisabledSlots:2039583}
execute at @p run tp @e[type=armor_stand,tag=rotation_helper] ~ ~ ~ facing entity @e[type=zombie,limit=1]

# Store the rotation in scoreboards
execute as @e[type=armor_stand,tag=rotation_helper] store result score @s x_rot run data get entity @s Rotation[0]
execute as @e[type=armor_stand,tag=rotation_helper] store result score @s y_rot run data get entity @s Rotation[1]
execute as @p store result score @s x_rot run data get entity @s Rotation[0]
execute as @p store result score @s y_rot run data get entity @s Rotation[1]

# Calculate the difference between the rotations
scoreboard players operation @p x_rot -= @e[type=armor_stand] x_rot
scoreboard players operation @p y_rot -= @e[type=armor_stand] y_rot

# Check if the rotations are similar, ±360°.
execute if score @p x_rot matches -2..2 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie
execute if score @p x_rot matches -362..-358 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie
execute if score @p x_rot matches 358..362 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie

# Output
execute if entity @p[tag=looking_at_zombie] run say looking at zombie
execute as @p if entity @s[tag=!looking_at_zombie] run say not looking at zombie

# Reset
kill @e[type=armor_stand,tag=rotation_helper]
tag @p remove looking_at_zombie

Answered by Fabian Röling on February 11, 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