Minecraft wiki entry for execute, associated text: Sets the execution rotation to face a given point, as viewed from its anchor (either the eyes or the feet)
Syntax
facing <pos> -> execute
facing entity <targets> <anchor> -> execute
Arguments
Option: facing <pos>
<pos>: vec3
Coordinate to rotate towards
Must be a three-dimensional coordinates with floating-point number elements. Accepts tilde and caret notations.
Option: facing entity <targets> <anchor>
<targets>: entity
The target(s) to rotate towards
Must be a player name, a target selector or a UUID.
<anchor>: entity_anchor (eyes or feet)
Whether to point at the target's eyes or feet
Result
Execution rotation is updated to face given position or targets
Unparseable if the argument is not specified correctly
Terminates if <targets> fails to resolve to one or more valid entities (named players must be online)
When multiple entities are selected, next subcommand is executed once in each execution rotation
Example
Executor rotates once to the left: execute facing ^1 ^ ^ run tp ~ ~ ~
All entities move one block in the direction of (0, 64, 0) (without changing their rotation): execute as @e at @s facing 0 64 0 run tp @s ^ ^ ^1
All entities move one block in the direction of (0, 64, 0) (with changing their rotation): execute as @e at @s facing 0 64 0 run tp ^ ^ ^1 ~ ~
All non player entities move one space in the direction of their nearest player (without changing their rotation): execute as @e[type=!player] at @s facing entity @p feet run tp @s ^ ^ ^1