4

I want to summon an arrow that is fired in the same direction as the player that it is summoned closest to, is this possible with just commands and as minimal command blocks as possible? (not one command block for like ever 30°)

Currently I have tried summoning it without gravity and right after it spawns teleporting it using /execute as @e[type=arrow,name="guardarrow",scores={lifetime=1..1}] at @s rotated as @p run tp @s ~ ~ ~ ~ ~ (lifetime scoreboard is just +1 per tick) which for about 0.5s rotates the arrow and then it rotates back, making this method useless. Using the Motion:[x.x,x.x,x.x] nbt doesn't work since it has to be able to go in all directions. So is there any way to make this work properly?

Also: When teleporting the arrows to move them they apparently don't hit things, they go straight through mobs

7
  • 2
    Possible duplicate of How to use ^^^ coordinate systems for motion nbt (minecraft) Commented Jul 20, 2019 at 21:48
  • I just voted to close this as duplicate, even though your question isn't exactly the same. The missing piece is that you first need to summon an armour stand (or anything) at the player's position and then use teleport facing to rotate it towards the mob you want to aim at, then use that as a starter for the arrow. Commented Jul 20, 2019 at 21:49
  • @Fabian Röling Does it work on multiple entities at a time though? I need it to make guards that shoot at nearby monsters, but from what I understand from this it won't work on multiple entities at once will it? If it does work on multiple entities by somehow adjusting it please tell me how. I'm kinda lost here... Commented Jul 20, 2019 at 22:43
  • @Fabian Röling I know it says in the other thread that it doesn't work for multiple players (which in my case are just replaced with armor stands) but it could work by replacing @p with tags, and I don't know how to exactly do this. I copied the commands and it doesn't work even with a single armor stand, so I very likely did something wrong trying to convert his commands. However, I know it is doing something, as my arrows stay midair rather than falling to the ground (I checked, and NoGravity nbt is off) Commented Jul 20, 2019 at 22:54
  • After messing with the commands a bit I have found that lowering the scale (or increasing) changes the arrows, but still not right. Either they fly really far in 1 of 2 directions, or they fall on the ground in front of the armor stand Commented Jul 20, 2019 at 23:20

1 Answer 1

1

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.

4
  • I am not sure as to how I have to add the id in this? Or rather, I don't understand where I have to put the id and how to make it the same as the guards' id. Commented Jul 21, 2019 at 15:06
  • The arrows are 'tagged' because they're summoned with a name, which is how I seperate these arrows. Same with these armor stand, it's not all armor stands, only those named "Guard" so players can use a name tag to 'create' guards. I already changed the commands to only activate on these specific entities, maybe by changing the selectors I made a typo, but I don't find anything wrong. These commands are new to me so I can't really tell what could be wrong though. Commented Jul 21, 2019 at 15:25
  • Isn't it possible to give them motion in the direction the guard is looking at the moment they start using the lifetime scoreboard I already have? (Meaning they only get motion for 1 tick and afterwards they aren't bound to any entity's rotation anymore) So they only have to check for the rotation of the closest guard. Wouldn't this be able to get rid of the id? (I'm also not sure which type of command block I need to use for this? Do I put them all on repeat or do I put them in a chain?) Commented Jul 21, 2019 at 15:45
  • Oh sorry, I forgot to remove the IDs from the beginning of the answer. You don't actually need them. And no, you don't need repeating cmdBlocks, you just apply the motion once. Except if you want to apply the motion repeatedly of course, then you loop the last three commands. Commented Jul 21, 2019 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.