Solved Reproduce knockback-like effect?

Discussion in 'Spigot Plugin Development' started by danablend, Jun 14, 2016.

  1. I'm trying to make the launch-back when player gets hit, just like knockback, I just need a bigger knockback. I know its vector stuff but I need it to make it the opposite direction of the attacking player, so how would I be doing this?
  2. That's really some basic math.

    Get the direction of the vector of the player you want to knock back. Negate that vector(thus pointing it "backwards"). And then you can optionally multiply the vector by a float to set the knockback strength.
    Now you need to apply the vector to the player as velocity.

    Gesendet von meinem Nexus 5 mit Tapatalk
  3. Yeah I was trying my way forwards, should it be something like this?
    Code (Text):
    victim.setVelocity(new Vector(-damager.getVelocity().getX(), distance, -damager.getVelocity().getZ()));
    Distance being the amount of blocks knocked up in the air.
  4. There's a function in the vector class to negate it's called negate. Also split up the statement, it's pretty long.
    But it looks right to me.

    Gesendet von meinem Nexus 5 mit Tapatalk
  5. Ok thanks.
  6. Ok I tried the following code but it only launches the guy in 1 direction (not the opposite of where the player is attacking)
    Code (Text):
    Vector knockbackVector = new Vector(-damager.getVelocity().getX() + distance, distance, -damager.getVelocity().getZ() + distance);
  7. Do some debugging. Let the plugin print your vector and check if it has the right values.
  8. damager.getVelocity() is returning 0 every time.
  9. Did you set the velocity?
    getVelocity() can't return 0 it can only return a vector. But the vector can have all components set to zero which basically would mean the players doesn't get pushed.

    Gesendet von meinem Nexus 5 mit Tapatalk
  10. No the player gets pushed to hell but in the same direction each time, the damager (one hitting the player) has a velocity where all values return 0, so it doesnt work subracting his velocity with the victim one.

    EDIT: Debug messages:
    Code (Text):
            System.out.println("x = " + -damager.getVelocity().getX());
            System.out.println("y = " + -damager.getVelocity().getY());
            System.out.println("z = " + -damager.getVelocity().getZ());
            Vector knockbackVector = new Vector(-damager.getVelocity().getX() + distance, distance, -damager.getVelocity().getZ() + distance);
            System.out.println("Vector = " + knockbackVector);
    X, Y and Z returns 0 each time. KnockbackVector returns 2.0, 2.0, 2.0 each time (Because "distance" variable is 2.0)
  11. Oh, you've misunderstood something.

    You need the direction of the player who gets attacked is looking in.
    Code (Text):

    Vector direction = damaged.getLocation().getDirection();
    You need to negate that and apply to the damaged player.

    EDIT: A better approach would be to take the direction the damager is looking and apply that to the damaged. But first try to get the above working.

    Gesendet von meinem Nexus 5 mit Tapatalk
  12. I cannot find any .negate() on the vectors.
  13. @danablend if the entity had the velocity in the damage event, then cancelling it would still give them the velocity. Either wait a tick or so or calculate it yourself.
  14. Ok I just found a great way of doing this and i can change stuff easily, this is the calculation:
    Code (Text):
            Vector knockbackVector = damager.getLocation().getDirection().multiply(distance*1.5).setY(distance);