1.16.5 Changing Player Facing Direction

Discussion in 'Spigot Plugin Development' started by Lavitra, Jul 18, 2021.

  1. So I want to teleport a player behind a player and make them face towards the player. I have looked into

    Code (Text):

    Location(World world, double x, double y, double z, float yaw, float pitch)
     
    but I do not understand the yaw and pitch parameters. How would I make it so that after the teleportation, the player looks towards the other player? What would I have to pass into the yaw and pitch parameters?
     
  2. Hey, the yaw and pitch are the rotation. You can press F3 to see your yaw and pitch.
    [​IMG]
     
  3. Thanks! Also I know I have the getFacing() property on the player but how would I go about calculating 2 blocks behind the player? As the player direction will always be changing, is there some handy way of getting the location 2 blocks behind the player?
     
  4. Yes there is!
    "Behind" translates to the opposite direction of the other player with an additional distance. Getting the opposite direction is just multiplying it with -1 and additional distance would be in your case multiplying with 2.
    Code (Text):
    Vector playerDir = otherPlayer.getLocation().getDirection();
    Vector twoBlocksBehind =  playerDir.clone().multiply(-2);
    Position teleportedPlayerLoc = twoBlocksBehind.toLocation(world).add(otherPlayerLoc);
    Now onto the other problem where you need to make the teleported player face the other player.
    We can just use the direction of the player that didn't teleport.
    Code (Text):
    teleportedPlayer.getLocation().setDirection(otherPlayer.getLocation().getDirection());
     
    • Winner Winner x 1
    • Useful Useful x 1
  5. Thank you so much!

    One final question - how would I get if some player was behind another player, say, suppose in a 120 degree angle and not like just half a block behind. If the player is in the 120 degree range behind him I would want to do something.
     
  6. Interesting idea. This requires some extra work. The general idea would be get the opposite direction just as we did previously and we calculate the degree difference between this direction and where the other player is. If the degree is below 60 (60 and not 120 because we can go 60 to the left or to the right, so 120 in total), you can do your stuff.
    It's probably better to show it through an example.
    behind.PNG
    This is basically just something I explained in my other post earlier.
    Suppose now we have another player at some other position. You normalize their location
    Code (Text):
    Vector otherPlayerVec = otherPlayer.getLocation().toVector().normalize()
    We can use the dot product to determine the "amount of change". Solving the dot product to the angle results in alpha=arccos(oppositeMainPlayerDir*otherPlayerVec)
    An example where the other player is inside the range:
    behind inside.PNG
    behind inside alpha.PNG
    And an example where he would be outside:
    behind outside.PNG
    behind outside alpha.PNG