1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Player->knockBack() offset

Discussion in 'Development' started by EndermannbugZJFC, Aug 19, 2020.

  1. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc

    Here is my code:
    PHP:
    $entity = (new class($p->getLevel(), new CompoundTag('',
        [
            new 
    ListTag("Pos", [
                new 
    DoubleTag("", ($args['pos'] ?? $p->asVector3())->getX()),
                new 
    DoubleTag(""$p->getY()),
                new 
    DoubleTag("", ($args['pos'] ?? $p->asVector3())->getZ())
            ]),
            new 
    ListTag("Motion", [
                new 
    DoubleTag(""1.0),
                new 
    DoubleTag(""1.0),
                new 
    DoubleTag(""1.0)
            ]),
            new 
    ListTag("Rotation", [
                new 
    FloatTag("", -90),
                new 
    FloatTag("", -90)
            ])
        ]
    )) extends \
    pocketmine\entity\Entity {
        public 
    $width 1;
        public 
    $height 2;
    });
    $entity->teleport(new \pocketmine\level\Location(($args['pos'] ?? $p->asVector3())->getX(), $p->getY(), ($args['pos'] ?? $p->asVector3())->getZ(), -90, -90$p->getLevel()));
    $p->knockBack($entity0, ($args['pos'] ?? $p->asVector3())->getX(), ($args['pos'] ?? $p->asVector3())->getZ(), $args['knockback'] ?? 1);
    $entity->close();
    I even tried to set the entity's rotation to other values but still not work
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    1) What do you want to do with that code?
    2) You needn't create new entity, you can use player you are knockbacking
    PHP:
    $p->knockBack($p0$x$z0.4 2); // 2x higher knockback
     
  3. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    Wait I can let player knockback himself :eek: k ty ill try
     
  4. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    Oof, it is still not working. I keep getting knocked to the right side instead of the centre as the video shown, is there a solution for that?
     
  5. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    You want be knockbacked to the spawn of the world?
     
  6. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    yes
     
  7. Taco

    Taco Spider Jockey

    Messages:
    41
    GitHub:
    taconoburrito
    You could knock the player really high in the air
    and then teleport them to the spawn point above the clouds, so they cant see theyve been teleported.
    and then make them fall to the spawn.
    it will create the illusion that theyve been knocked back to spawn, but idk if thats what ur going for. just a suggestion
     
  8. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Here is simple code you can use, maybe it will need change some constants, but it worked fine when I tested it.

    PHP:
    /** @var Player $player */
    $spawn $player->getLevel()->getSpawnLocation();

    $maxKnockBack 5;

    $motion $spawn->subtract($player);
    if(
    abs($motion->getX()) > $maxKnockBack) {
        
    $motion->= ($motion->getX() <=> 0) * $maxKnockBack;
    }
    if(
    abs($motion->getZ()) > $maxKnockBack) {
        
    $motion->= ($motion->getZ() <=> 0) * $maxKnockBack;
    }

    $dist = (new Vector2($player->getX(), $player->getZ()))->distance($spawn->getX(), $spawn->getZ());

    $player->knockBack($player0$motion->getX(), $motion->getZ(), min((mt_rand(1015) / 10), $dist 10));
     
    Taco likes this.
  9. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    k thank you very much
     
  10. Taco

    Taco Spider Jockey

    Messages:
    41
    GitHub:
    taconoburrito
    that is very cool
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.