setVelocity() Help

Discussion in 'Plugin Development' started by heifinator, Nov 12, 2011.

Thread Status:
Not open for further replies.
  1. Offline


    Basically I need to change the players velocity based on their current velocity, I cannot just use Multiply() because I don't want to change their Y velocity, just the X/Z velocity.

    I am having some trouble, any help would be appreciated. Right now Im just getting the velocity for getX() and getZ() multiplying them and trying to setVelocity() the new values, but no luck.

  2. Offline


    What I've used to eject items (which doesn't really matter since both inherit the setVelocity() method from the Entity class) away for signs, is this:

    Vector vect getVelocity(loc);

    if (
    vect != null) {


    And the getVelocity(Location) method:

    private Vector getVelocity(Location loc) {

    Block block loc.getBlock();

        if (
    block.getState() instanceof Sign) {

    Sign sign = (Signblock.getState();
    byte d sign.getData().getData();

            switch (
    d) {

    0x2://North, z goes down
    return new Vector(00, -0.2);

    0x3://South, z goes up
    return new Vector(000.2);

    0x4://West, x goes down
    return new Vector(-0.200);

    0x5://East, x goes up
    return new Vector(0.200);



    Basically, I'm getting the velocity vector, and adding to it another vector (which depends on the sign's orientation, but that's just part of my plugin, don't worry about that).
    L3N likes this.
  3. Offline


    My favorite vector is
     Player player = event.getPlayer();
        	 Vector dir = player.getLocation().getDirection();
              Vector vec = new Vector(dir.getX() * 0.8D, 0.8D, dir.getZ() * 0.8D);
    If you use it onPlayerInteract it works like a Fly Mod :]
  4. Offline


    @heifinator The reason this fails is that the 'getVelocity' in onMove is failing for players. This is because clients only send the new location of the player, and not the velocity. This shock-motion system is smoothed out, so you don't notice it. Your best bet is using the from and to to get the velocity of the player, multiply that, and set to using this new velocity.
    Vector diff = new Vector(to.getX() - from.getX(), to.getY() - from.getY(), to.getZ() - from.getZ());
    diff = diff.multiply(1.1, 1.1, 1.1);
    to = from.clone().add(diff.getX(), diff.getY(), diff.getZ());
    But then you risk an infinite increase of the velocity (it's getting 10% higher each tick), which can cause really weird things to happen. Best is to set a limit. (diff.length() < 0.9)
    justcool393 and emericask8ur like this.
Thread Status:
Not open for further replies.

Share This Page