#determine the coordinates of the current pos
$execute as @s[tag=$(tag)$(score)] store result score @s enemy_x run data get entity @s Pos[0] 1000
$execute as @s[tag=$(tag)$(score)] store result score @s enemy_y run data get entity @s Pos[1] 1000
$execute as @s[tag=$(tag)$(score)] store result score @s enemy_z run data get entity @s Pos[2] 1000

#spawn a marker and teleport it facing player's direction
$execute at @s run summon marker ~ ~ ~ {Tags:["$(tag)_marker"]}
$execute as @e[type=marker,tag=$(tag)_marker,sort=nearest,limit=1] at @s run teleport @s ~ ~ ~ facing entity @p
$execute as @e[type=marker,tag=$(tag)_marker,sort=nearest,limit=1] at @s run teleport @s ^ ^$(height) ^$(speed)

#determine the coordinates of the new pos
$execute as @s[tag=$(tag)$(score)] store result score @s target_x run data get entity @e[type=marker,tag=$(tag)_marker,sort=nearest,limit=1] Pos[0] 1000
$execute as @s[tag=$(tag)$(score)] store result score @s target_y run data get entity @e[type=marker,tag=$(tag)_marker,sort=nearest,limit=1] Pos[1] 1000
$execute as @s[tag=$(tag)$(score)] store result score @s target_z run data get entity @e[type=marker,tag=$(tag)_marker,sort=nearest,limit=1] Pos[2] 1000

#determine the vector
$execute as @s[tag=$(tag)$(score)] run scoreboard players operation @s target_x -= @s enemy_x
$execute as @s[tag=$(tag)$(score)] run scoreboard players operation @s target_y -= @s enemy_y
$execute as @s[tag=$(tag)$(score)] run scoreboard players operation @s target_z -= @s enemy_z

#move enemy in the direction of the new position
$execute as @s[tag=$(tag)$(score)] at @s store result entity @s Motion[0] double 0.001 run scoreboard players get @s target_x
$execute as @s[tag=$(tag)$(score)] at @s store result entity @s Motion[1] double 0.001 run scoreboard players get @s target_y
$execute as @s[tag=$(tag)$(score)] at @s store result entity @s Motion[2] double 0.001 run scoreboard players get @s target_z

$kill @e[type=marker,tag=$(tag)_marker,sort=nearest]
