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

Solved How I can knockback all players from a certain player.

Discussion in 'Development' started by Yexeed, Nov 26, 2017.

  1. Yexeed

    Yexeed Slime

    Messages:
    76
    For example, the "certain player" is me. How to knockback all players off me when they come near to me?

    According to the rules of the forum i have to attach my attempts. But everything i have only one guess. I think i can do it using distance, setMotion, getDirectionVector()->subtract()

    And here is my attempt
    PHP:
    public function onMove(PlayerMoveEvent $e){
            if(
    $e->getPlayer()->isOp()){
                
    $pl $e->getPlayer();
                foreach(
    $this->getServer()->getOnlinePlayers() as $players){
                    if(
    $players->distance(new Vector3($pl->x$pl->y$pl->z)) <= 5){
                        if(!
    $players->isOp()){
                            
    $players->setMotion($players->getDirectionVector()->subtract());
                        }
                    }
                }
            }
        }
     
    Last edited: Nov 26, 2017
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Any previsious attempts?
     
  3. Yexeed

    Yexeed Slime

    Messages:
    76
    Updated
     
  4. Yexeed

    Yexeed Slime

    Messages:
    76
    Updated again, lmao
     
  5. Yexeed

    Yexeed Slime

    Messages:
    76
    So, maybe someone will write something? Because code i provided is pushing players in direction that they are look. How to reverse this?
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I think you meant:
    PHP:
    $f 1.8;
    $players->setMotion($pl->subtract($players)->normalize()->multiply($f));
    $f is proportional to the knockback distance.
     
    jasonwynn10 likes this.
  7. Yexeed

    Yexeed Slime

    Messages:
    76
    thanks alot again
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    That's not an attempt. You just copied some code without undrestanding.
     
    jasonwynn10 likes this.
  9. Yexeed

    Yexeed Slime

    Messages:
    76
    Yes, I asked some tips from my friend, he gave me "setMotion", "distance", and "subtract". Since I dont know Vector3 base logic I dont know how it works. But I attempted to use it in practice anyway
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Player->getDirectionVector() is the vector from the player to the block he is facing, while Player->subtract(Player) gives the vector from one player to another.
     
    Yexeed, jasonwynn10 and Muqsit like this.
  11. Yexeed

    Yexeed Slime

    Messages:
    76
    thanks for the explanation
     
    jasonwynn10 likes this.
  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.