4

So I have this code:

Vector vel = playerA.getVelocity(); playerB.setVelocity(vel);

Which gives playerB the velocity of playerA. The problem is that playerB often gets unsycned from playerA's position and if the players are more than a block or so away from each other, playerB doesn't get moved at all unless playerA jumps. Teleporting playerB to playerA is very glitchy as they need to be able to move the mouse.

Can anybody point me in the right direction to fixing this?

ouflak
  • 2,458
  • 10
  • 44
  • 49
byAdam
  • 51
  • 6
  • Isn't there an event for entering a new block? You could set the velocity as soon as they enter the new block. – Jire Jan 24 '16 at 11:17
  • Could you make 1 player ride the other, I don't think the connection will be fast enough to ever do what you want to do, you could also use spectator mode if that would work. – Dallen Feb 09 '16 at 01:49

2 Answers2

1

I'm assuming you're trying to build some code that will make playerB follow playerA around. Why not calculate the difference of locations between the two players, and use that to construct a new vector?

For instance:

 Location difference = playerA.getLocation().subtract(playerB.getLocation());
 playerB.setVelocity(difference.toVector());

Therefore, this will constantly (constantly meaning each time the piece of code is called) set the velocity of playerB to this new vector, and make him advance in that direction.

Pyves
  • 6,333
  • 7
  • 41
  • 59
1

I don't think using velocity will ever lead you to success. Instead I'd try to use teleport, but override the Yaw and Pitch fields of playerA's location with playerB's values to allow "free mouse movement":

@EventHandler
public void onMove(PlayerMoveEvent event)
{
    if (event.getPlayer().equals(playerA))
    {
        Location loc = event.getPlayer().getLocation();
        loc.setPitch(playerB.getLocation().getPitch());
        loc.setYaw(playerB.getLocation().getYaw());
        playerB.teleport(loc);
    }
}
Zahlex
  • 628
  • 2
  • 8
  • 18