How to make armor stands follow you when in a specific vehicle in 1.17.1

I have tried to make an artillery cannon data pack. I want it so that when sitting in a minecart with tag "gun" multiple armor stands with blocks on their heads make the cannon body move where the player does.

I tried to do this:

/execute as @e[type=minecraft:minecart, tag=gun] at @p[distance=..1] run tp @e[type=armor_stand, tag= gunmain, distance=..2] ^ ^ ^3 ~ ~

It does not seem to work reliably but when it does work the armor stand gets teleported only once. Does anyone have a solution for this?

Thanks in advance :)


Solution 1:

By examining your command, it is clear that all armor stands are being teleported to the tiny space 3 blocks ahead of you, which I don't think is what you want to achieve.

I am afraid you have to deal with the cannon parts separately. E.g. The cannon composes of a stone block and a glass block.

execute as @e[type=minecraft:minecart, tag=gun] at @p[distance=..1] run tp @e[type=armor_stand, tag= stone, distance=..2] ^ ^ ^1 ~ ~
execute as @e[type=minecraft:minecart, tag=gun] at @p[distance=..1] run tp @e[type=armor_stand, tag= glass, distance=..2] ^ ^ ^2 ~ ~

You may also want to rotate the armor stand facing the same way as the player. I can provide you additional support shall the need arise.

Having that said, I concern that the method you employ may not be the most efficient and the most secure. First of all, teleporting is not exactly an efficient command. Secondly, what if the cannon parts are missing? What about the distance between the cannon parts? And would the structure of cannons be messed up if several cannons are placed rigidly?

Here is a method I have come up with. Minecart + COAS Method Brief overview -Detect if player is on a minecart -Give the player carrot_on_a_stick if so -Execute at the players on the minecart and have right clicked -Ka Boom

execute at @e[type=minecart] run execute if entity @e[type=player,distance=..0.5] run item replace entity @p weapon.mainhand with carrot_on_a_stick 

(Make sure you have a scoreboard variable linked to used:minecraft.carrot_on_a_stick)

execute at @e[type=minecart] run execute as @p[scores={right_click=1}] run say ShootAmmo

Lastly, reset the scoreboard right click

scoreboard players reset @a right_click

Put all the above in a tick function and you should be able to get a fully functioning turret.

On the topic of the turret model, I strongly advise you to use an App called BlockBench, which helps to create models and textures for Minecraft. Simply set up a resource pack and make your life easier, it is not difficult at all, I promise.