Make an arrow follow a target!

Discussion in 'Plugin Development' started by HeavyMine13, Mar 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    HeavyMine13

    Hey! So I need to make arrows that follow the nearest entity and kills it instantly! How can I do it?
     
  2. Offline

    Noxyro


    You should know how this forum works.
    Give us some existing code and an error and we are willing to help.
    For everything else: Google is your friend.
     
    HeavyMine13 and user_90854156 like this.
  3. Offline

    blablubbabc

    Run a task which changes the arrows veloctiy/direction each tick to let it fly either directly towards the target, or maybe only a smal step towards the target (so that the arrows flies in a curve rather than a direct line).

    As an example: https://github.com/blablubbabc/Boun...ubbabc/bouncingarrows/BouncingArrows.java#L99

    Though the particles in this version do no longer work, and you might consider creating one repeating task instead of many delayed tasks for each flying arrow.
     
  4. Offline

    SuperOmegaCow

    blablubbabc Noxyro HeavyMine13
    Copy and paste from SethBling's plugin.
    Code:java
    1. public class HomingTask
    2. extends BukkitRunnable
    3. {
    4. private static final double MaxRotationAngle = 0.12D;
    5. private static final double TargetSpeed = 1.4D;
    6. Arrow arrow;
    7. LivingEntity target;
    8.  
    9. public HomingTask(Arrow arrow, LivingEntity target, Plugin plugin)
    10. {
    11. this.arrow = arrow;
    12. this.target = target;
    13. runTaskTimer(plugin, 1L, 1L);
    14. }
    15.  
    16. public void run()
    17. {
    18. double speed = this.arrow.getVelocity().length();
    19. if ((this.arrow.isOnGround()) || (this.arrow.isDead()) || (this.target.isDead()))
    20. {
    21. cancel();
    22. return;
    23. }
    24. Vector toTarget = this.target.getLocation().clone().add(new Vector(0.0D, 0.5D, 0.0D)).subtract(this.arrow.getLocation()).toVector();
    25.  
    26. Vector dirVelocity = this.arrow.getVelocity().clone().normalize();
    27. Vector dirToTarget = toTarget.clone().normalize();
    28. double angle = dirVelocity.angle(dirToTarget);
    29.  
    30.  
    31.  
    32. double newSpeed = 0.9D * speed + 0.14D;
    33. if (((this.target instanceof Player)) && (this.arrow.getLocation().distance(this.target.getLocation()) < 8.0D))
    34. {
    35. Player player = (Player)this.target;
    36. if (player.isBlocking()) {
    37. newSpeed = speed * 0.6D;
    38. }
    39. }
    40. Vector newVelocity;
    41. Vector newVelocity;
    42. if (angle < 0.12D)
    43. {
    44. newVelocity = dirVelocity.clone().multiply(newSpeed);
    45. }
    46. else
    47. {
    48. Vector newDir = dirVelocity.clone().multiply((angle - 0.12D) / angle).add(dirToTarget.clone().multiply(0.12D / angle));
    49. newDir.normalize();
    50. newVelocity = newDir.clone().multiply(newSpeed);
    51. }
    52. this.arrow.setVelocity(newVelocity.add(new Vector(0.0D, 0.03D, 0.0D)));
    53. this.arrow.getWorld().playEffect(this.arrow.getLocation(), Effect.SMOKE, 0);
    54. }
    55. }


    Code:java
    1. public class HomingArrows
    2. extends JavaPlugin
    3. implements Listener
    4. {
    5. public void onEnable()
    6. {
    7. getServer().getPluginManager().registerEvents(this, this);
    8. }
    9.  
    10. public void onDisable()
    11. {
    12. }
    13.  
    14. @EventHandler
    15. public void eventArrowFired(EntityShootBowEvent e)
    16. {
    17. if (((e.getEntity() instanceof LivingEntity)) && ((e.getProjectile() instanceof Arrow)))
    18. {
    19. LivingEntity player = e.getEntity();
    20.  
    21. double minAngle = 6.283185307179586D;
    22. Entity minEntity = null;
    23. for (Entity entity : player.getNearbyEntities(64.0D, 64.0D, 64.0D)) {
    24. if ((player.hasLineOfSight(entity)) && ((entity instanceof LivingEntity)) && (!entity.isDead()))
    25. {
    26. Vector toTarget = entity.getLocation().toVector().clone().subtract(player.getLocation().toVector());
    27. double angle = e.getProjectile().getVelocity().angle(toTarget);
    28. if (angle < minAngle)
    29. {
    30. minAngle = angle;
    31. minEntity = entity;
    32. }
    33. }
    34. }
    35. if (minEntity != null) {
    36. new HomingTask((Arrow)e.getProjectile(), (LivingEntity)minEntity, this);
    37. }
    38. }
    39. }
    40. }
     
  5. Offline

    ZeusAllMighty11

  6. Offline

    Garris0n

    SuperOmegaCow Don't just paste other people's code in without any credit whatsoever...

    And don't spoonfeed people either.
     
    Mattkx4 and HeavyMine13 like this.
  7. Offline

    HeavyMine13

    True, ill make my own code!
     
Thread Status:
Not open for further replies.

Share This Page