Reputation: 61
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?
Upvotes: 4
Views: 1381
Reputation: 648
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);
}
}
Upvotes: 1
Reputation: 6483
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.
Upvotes: 1