How to make arrows to not bounce back but explode when they hit an entity?



  • I made arrows explode when they have the {inGround:1b} tag but the problem is that they won't explode when they hit entities, instead they bounce off them and hit the ground then explode.

    How do I make it so that arrows won't bounce back but explode when they hit an entity?



  • I believe that I have found a perfect solution after numerous attempts on creating a perfect projectile.

    The solution is rather simple, it involves the use of marker entities.

    Brief Introduction

    Marker entities are added into the java-edition of Minecraft in the 1.17 update. They are intended to satisfy all basic properties of entities but have the minimum behaviors- no sounds - no models - unseen - unmoved - can only be created with the /summon command. These qualities dictate that marker entities are simply dots in a 3D world, which are small, effective, and as minimally space-consuming as possible. In other words, they work very well with commands and data packs.

    Here is a template of my work. In a shoot.mcfunction

    summon marker ^ ^1.3 ^1 {Tags:["bullet","new_bullet"],Invulnerable:1b}
    execute as @e[tag=new_storm] at @s rotated as @p run tp @s ~ ~ ~ ~ ~
    tag @e[tag=new_storm,limit=1,sort=nearest] remove new_storm
    

    In a bullet_behaviour.mcfunction (tick function)

    particle flame ~ ~ ~ 0 0 0 0.01 10 force @a
    tp @s ^ ^ ^10
    execute unless block ^ ^ ^-1 #permeable positioned ^ ^ ^-1 run kill @s
    execute unless block ^ ^ ^-2 #permeable positioned ^ ^ ^-2 run kill @s
    ...
    execute unless block ^ ^ ^-10 #permeable positioned ^ ^ ^-10 run kill @s
    

    execute positioned ^ ^ ^-2.5 if entity @e[distance=0..2.5,type=!marker] run function kill @s
    execute positioned ^ ^ ^-5 if entity @e[distance=0..2.5,type=!marker] run function kill @s
    execute positioned ^ ^ ^-7.5 if entity @e[distance=0..2.5,type=!marker] run function kill @s
    execute positioned ^ ^ ^-10 if entity @e[distance=0..2.5,type=!marker] run function kill @s

    Note - #permeable consists of air, cave air, cobweb, water, grass, tall_grass and snow

    Code Explanation

    The method is surprisingly simple. It summons a marker entity, rotates it to where the nearest player is looking, and teleports it forward 10 blocks every tick, resulting in high traveling speed. While many may doubt if the projectile would skip through all the blocks in between, the solution is to check for all blocks possibly not air by using the positioned command 10 times, searching for the 10 blocks that the marker has just traveled and to see if there are non-air blocks. If so, run something at that position.

    Why it is better than the arrow method

    This method in my opinion is much better than the arrow method, which includes complicated manipulation of motion tags, in the process requiring many data storage commands and scoreboards which are tedious and slow. Another poor aspect of the arrow method is that the arrows can be very inconsistent. For example, when I try to stop the arrows when there are entities nearby, the Motion tags of arrows do not seem to work well alongside entity selectors, sometimes resulting in the negation of the entity selectors totally. On the contrary, teleportation works well with selectors. They are very precise and never fail to detect entities nearby markers. The last thing to mention about arrows is that they bounce off entities, a quality that makes arrows unsuitable for making bullets since bullets won't just bounce off towards you when they hit the targets.

    Why you should use the marker method

    1. It is consistent.
    2. It is fast
    3. The method itself is simple and codes are easy to read
    4. Very customizable - I will explain down below.

    Customization

    Needless to say, bullets come in different hit effects, speeds, particle effects and maximum traveling distances. The Marker method provides customization over all the aspects mentioned above.

    Speed Manipulation

    Simply change the distance the marker teleports each tick. Note that more lines of codes must be put into detecting blocks in between each teleportation. For example, to allow for tp ^ ^ ^20 you must add 10 lines of extra code to check for blocks.

    execute unless block ^ ^ ^-11 #permeable positioned ^ ^ ^-11 run kill @s
    ...
    execute unless block ^ ^ ^-20 #permeable positioned ^ ^ ^-20 run kill @s
    

    This is why I would not recommend going over the 10 block teleportation threshold - more codes, more lags.

    Hit effects

    This is a no-brainer --- change the kill @s to any function you like.

    Particle effects

    Change the flame particle to something else.

    Maximum traveling distance

    A great way to prevent lag. Create a scoreboard called lived scoreboard objectives add lived dummy Everytime marker teleports, increase this value by one. Kill markers with score that is too high (an integer of your own choice).

    Conclusion

    I believe The Marker method is far superior than the arrow method due to all the above reasons. If you are trying to create a gun, I would strongly recommend you to try out my method.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2