You said in the comments that you have multiple "guards" that each shoot at targets, potentially at the same time. That complicates the matter a bit, compared to this answer.
You need the scoreboards x
, y
and z
, all of type dummy
.
As a first step whenever you want something to fire I suggested creating a dummy entity that you can rotate towards the target, but I guess rotating the shooting entities themselves is probably better in your case:
execute as @e[shooter>] at @s run tp @s ~ ~ ~ facing entity @e[<selector for target>,limit=1]
Then you do basically the same as in my other answer, except that some things have to be done relative to the shooter, because just tagging the arrows doesn't work if there are multiple at the same time.
execute as @e[<shooter>] at @s run summon arrow ^ ^ ^1 {NoGravity:1}
execute as @e[type=arrow] store result score @s x run data get entity @s Pos[0] 10
execute as @e[type=arrow] store result score @s y run data get entity @s Pos[1] 10
execute as @e[type=arrow] store result score @s z run data get entity @s Pos[2] 10
execute as @e[type=armor_stand] store result score @s x run data get entity @s Pos[0] 10
execute as @e[type=armor_stand] store result score @s y run data get entity @s Pos[1] 10
execute as @e[type=armor_stand] store result score @s z run data get entity @s Pos[2] 10
execute as @e[type=armor_stand] at @s positioned ^ ^ ^1 run scoreboard players operation @e[type=arrow,distance=0] x -= @s x
execute as @e[type=armor_stand] at @s positioned ^ ^ ^1 run scoreboard players operation @e[type=arrow,distance=0] y -= @s y
execute as @e[type=armor_stand] at @s positioned ^ ^ ^1 run scoreboard players operation @e[type=arrow,distance=0] z -= @s z
execute as @e[type=arrow] store result entity @s Motion[0] double 0.1 run scoreboard players get @s x
execute as @e[type=arrow] store result entity @s Motion[1] double 0.1 run scoreboard players get @s y
execute as @e[type=arrow] store result entity @s Motion[2] double 0.1 run scoreboard players get @s z
You'll probably also want to tag the arrows in some way, so that you don't set their Motion
to 0 in the last step if they were fired from a bow or if you've already applied the motion to them (except if you want that).
Keep in mind that arrows lose a bit of speed over time, so it would at some point stop in midair. And the target can of course move. You can also adjust the scale factors (10
and 0.1
) if you want the arrows to be faster.
teleport facing
to rotate it towards the mob you want to aim at, then use that as a starter for the arrow.