Solved Moving an entity from point a to b in 3d in a line

Discussion in 'BungeeCord Plugin Development' started by nelson2tm, Apr 24, 2017.

Thread Status:
Not open for further replies.
  1. 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);
        }

    }
     
    #1 nelson2tm, Apr 24, 2017
    Last edited: Apr 24, 2017
  2. MiniDigger

    Supporter

    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)
     
    • Useful Useful x 1
    • Useful Useful x 1
  3. 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));
     
Thread Status:
Not open for further replies.