Im trying to make an arrow follow a player. I tried this: Code: Code (Text): public class HomingTask extends BukkitRunnable { Arrow arrow; Player target; public HomingTask(Arrow arrow, Player p) { this.arrow = arrow; target = p; long delay = 1; runTaskTimer(Main.plugin, delay, delay); } public void run() { Location ploc = target.getLocation(); Vector vec1 = new Vector(ploc.getBlockX(), ploc.getBlockY(), ploc.getZ()); arrow.setVelocity(vec1); arrow.getWorld().spawnParticle(Particle.CLOUD, arrow.getLocation(), 5); } } Instead, an arrow shoots at 100 blocks per second location not following the player and lagging the server because of particles being constantly spawned.
I am pretty sure new Vector(x, y, z) is the speeds. Not the location so try this: (Warning I haven't tested it may have typos) Code (Text): public class HomingTask extends BukkitRunnable { Arrow arrow; Player target; public HomingTask(Arrow arrow, Player p) { this.arrow = arrow; target = p; long delay = 1; runTaskTimer(Main.plugin, delay, delay); } public void run() { Location ploc = target.getLocation(); Location arrowLoc = arrow.getLocation(); double speedMultiplier = 1.0;// Adjust speed as necessary double xDif = ploc.getX() - arrowLoc.getX(); double yDif = ploc.getY() - arrowLoc.getY(); double zDif = ploc.getZ() - arrowLoc.getZ(); double totalDif = xDif + yDif + zDif; Vector vec1 = new Vector(xDif / totalDif, yDif / totalDif, zDif / totalDif).multiply(speedMultiplier); arrow.setVelocity(vec1); arrow.getWorld().spawnParticle(Particle.CLOUD, arrow.getLocation(), 5); } }
You have to substract the foot (the arrow) from the peak (the player) of the vector to get the right vector. Thats just simple mathematics.
this is not how you use the velocity... have you ever learned anything about vectors? if not, I would suggest learning about the math first before trying to apply vectory in your plugins... EDIT: or just can just be spoon fed by someone on the internet, I still suggest you to try to understand what @GreenBeard did there...
............. learn vectors first... or atleast look at the javadocs... https://hub.spigotmc.org/javadocs/b.../Vector.html#subtract(org.bukkit.util.Vector)
@SeamusMcFreak Check out this tutorial: https://www.spigotmc.org/threads/tutorial-vector-for-beginners.68194/ or this one https://www.spigotmc.org/threads/vector-programming-for-beginners.68203/
Thank you for sending me a tutorial. I will learn Vector and then complete my plugin Thread solved and closed.