Rotate an entity to look at the same place as the player [duplicate]

I try to make a tagged Armor Stand look at the same place as the player, but I can't find a way to do it.

For example, if the player looks in the direction of the (x, y, y) block, the Armor Stand should look at the (x, y, z) block.

For now I wrote the following command, but it only rotates the Armor Stand in the same direction as the player, it doesn't make it look at the same place:

/execute as @e[tag=my_armor_stand] at @s rotated as @p run teleport @s ~ ~ ~ ~ ~

If this should take more than one command, it is also okay.


Solution 1:

  1. Summon an Armor Stand with a specific tag, Invulnerable, and NoGravity.
    • summon minecraft:armor_stand ~ ~ ~ {Invulerable:1b,NoGravity:1b,Tags:["tag1"]}
  2. Every tick, teleport the Armor Stand to the relative position of the player
    • execute as @p at @s anchored eyes run tp @e[tag=tag1] ^ ^ ^2
  3. Every tick, teleport your own Armor Stand to face the new one
    • execute as @e[tag=tag2] at @s run tp @s ~ ~ ~ facing @e[tag=tag1,limit=1]
  4. Once you're done, kill the Armor Stand.
    • kill @e[tag=tag1]

Make sure to replace tag1 and tag2 with your actual tag names.

Edit: For looking at the block, you want to send out a different armor stand tag3 from the player and when that hits a block, teleport your tag2 armor stand to facing that one's location. Then repeat the process, sending out tag3 and teleportinng tag2 to face it until you're done. This is called raycasting