In order to trigger a chat message "from" NPCs - say villagers - when approaching them, I am using an always-active repeat-command block (A), a redstone-signal-sensitive impulse-command block (B) and a redstone comparator between them, pointing from A to B.
Since I want to let the NPCs run free, I can't use absolute coordinates, so what I'm currently doing is:
A: execute at @e[name=Gary] if entity @p[distance=..2] B: execute at @e[name=Gary] run tellraw @a[distance=..2] "Hey!"
This results in the chat message "Hey!" to appear once for every player with distance 2 blocks or less to an entity named Gary as soon as a player is detected in that space.
Since it is odd that Gary talks to people in front of him as well as to people behind him, is it possible to alter the commands in a way that the center of the area in which players are detected (and in which players have to be to get the message) is not at exactly Gary's position, but 1 block in front of him?
If Gary always stood in the same spot, it would be easy by using absolute coordinates like this:
A: execute if entity @p[x=x_,y=y_,z=z_,distance=..2] B: tellraw @a[x=x_,y=y_,z=z_,distance=..2] "Hey!"
x_, y_ and z_ being the desired coordinates 1 block in front of Gary.
Since version 1.13 it is possible to use caret notation for coordinates in some commands making them relative coordinates considering the command's executor's viewing direction, but it seems that target selector arguments only support absolute coordinates in Java edition.