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

API for teleporting behind

Discussion in 'Development' started by BruhLol, Apr 8, 2018.

  1. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    Is there a function to get back position of a player? I want a player to teleport behind a player where the player isnt facing, how can i do this?
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    What you could do is getting the direction vector of the player to teleport to, and subtract the vector you get from the position of the player to teleport to.
    PHP:
    $player->teleport($target->subtract($target->getDirectionVector()));
    You can multiply the direction vector should you want to teleport the player at a further distance behind the target player. Do note that if the target is facing downwards, the player will teleport above the target.
     
    BruhLol likes this.
  3. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    genius
     
    Sandertv likes this.
  4. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    You can check the direction of the player and tp to the same coords but with the x or the z -1 or +1 of the players position
     
  5. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    This is more work then this
     
  6. azk_

    azk_ Spider Jockey

    Messages:
    49
    *teleports behind you*
    - Nothing personal, kid.
     
    Sandertv likes this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You may want to rotate the vector in case the player is facing the floor/sky. In this case, it is more appropriate to calculate a new vector using the target's yaw only.

    PHP:
    $yaw $target->getYaw();
    $dx sin(deg2rad($yaw));
    $dz = -cos(deg2rad($yaw));
    // sin(y) ** 2 + cos(y) ** 2 === 1, no need to normalize
    $player->teleport($target->add($dx0$dz));
     
    BruhLol, Tee7even and jojoe77777 like this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    BruhLol 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.