How would I move an entity from point a to b smoothly in 3d (x, y and z), in a straight line? I currently have this code which successfully makes the entity move to a player, but it only works on the x and z axis. What can I do make the entity move in 3d to point b, so also the y axis? Here's my current test code: Code (Text): LivingEntity armorstand = (LivingEntity) Bukkit.getWorld("world").spawn(new Location(Bukkit.getWorld("world"), 0, 100, 0), ArmorStand.class); armorstand.setGravity(false); id = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new BukkitRunnable() { @Override public void run() { Location point = armorstand.getLocation(); Location target = Bukkit.getPlayer("nelson2tm").getLocation(); double angle = Math.atan2(target.getZ() - point.getZ(), target.getX() - point.getX()); point.add(Math.cos(angle) * 0.07, 0, Math.sin(angle) * 0.07); armorstand.teleport(point); } }
don't teleport, that will look ugly as hell. calculate the velocity vector and set that. (B-A and scale that to the desired speed)
yup, as said above, you'll most likely want to use a vector to move the stand. some tutorials out there on vector manipulation, such as https://bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/ https://www.spigotmc.org/wiki/vector-programming-for-beginners/#example
Thank you, it now works now. Code (Text): Vector pos = armorstand.getLocation().toVector(); Vector target = Bukkit.getPlayer("nelson2tm").getLocation().toVector(); Vector velocity = target.subtract(pos); armorstand.setVelocity(velocity.normalize().multiply(0.07));