Minecraft – way to detect a player with relative coordinates to the command block

minecraft-commandsminecraft-java-edition

Using the command: execute if entity @p[x=21 ,y=56 ,z=-132 ,distance=..4.5] works but won't work if I use relative coordinates. e.g. command:execute if entity @p[x=~ ,y=~ ,z=~ ,distance=..4.5]
I need a way to detect the player with relative coordinates from the block executing the command. I tried using dx=/dy=/dz=it does seem to work partially but I can't figure out the right parameters to use in @p[ ] please help.

Best Answer

You almost had it, /execute is the solution, but with positioned:

/execute positioned ~1 ~2 ~3 if entity @p[distance=..4.5] run …