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
Best 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.