Minecraft – How to execute a command at a player head

minecraft-commandsminecraft-java-edition

I am using a repeat command to simulate water breathing with particles, but the particles don't look realistic when the player is swimming with crtl activated, when the player is in walk position it Works perfectly but I didn't find any relative position with x y and z to make it good for both at the same time

The command that I'm using:

/execute as @a at @s run execute if block ~ ~2 ~ water run execute if score aguaa aguaa < 80 constante run particle minecraft:bubble_column_up ~ ~1.8 ~ 0 0 0 0.1 5 force

enter image description here

Best Answer

/execute anchored eyes can do this (and it seems to be the only way to detect when a player is swimming).
This command does something at the nearest player's head position, which is properly adjusted to swimming, crawling, elytra and sneaking:

/execute as @p at @s anchored eyes run <command>

Now the trick: The anchor only works with the rotationally relative coordinates (^ ^ ^), not with the axis relative coordinates (~ ~ ~), so if you don't use those coordinates in your "<command>" part anyway, you need to align it like this:

/execute as @p at @s anchored eyes positioned ^ ^ ^ run <command>

The "as" part is also needed, because the anchor needs an entity reference to know the eye height.