Change the Player's Yaw and Pitch to face another Player

Discussion in 'Spigot Plugin Development' started by TheNewTao, Feb 2, 2016.

  1. Hi,

    I am trying to teleport and make him face the other player. How can I calculate the pitch and yaw that will make him face the player? Could I get the other player's yaw and pitch and invert it?

    Thank you
     
  2. If you are trying to teleport and make the player face another, this is an easy way:

    Code (Text):
                    Player player = personYouWantToTeleport;

                    Player target = playerYouWantToFace;

                    Vector targetDirection = target.getLocation().getDirection(); // Direction target is facing
                                                                   
                    Vector newFacingTargetDirection = (targetDirection.multiply(-1)); // opposite direction

                    Location location = new Location(world, xCord, yCord, zCord);

                    location.setDirection(newFacingTargetDirection);  //set direction to opposite direction
                    location.setPitch(0); //if they are on flat ground set this to 0 so they are looking straight ahead (optional)

                    player.teleport(location); //teleported facing opposite direction of target!

               

    Basically getting the direction and inverting it as you said
     
  3. You need to get the vector between them, not use a direction that already exists:
    Code (Java):
    Vector diff = target.getEyeLocation().toVector().subtract(player.getEyeLocation().toVector());
    diff.normalize(); //set to a distance of 1
    Location fixed = player.getLocation();
    fixed.setDirection(diff); //set the direction to the difference of locations between the two players
    //teleport
     
    • Useful Useful x 2
  4. This does not work, here the teleported player will look in the opposite direction of the look direction of the player he should face.
     
  5. What? The player would teleport facing the target.. already tested it
     
    • Optimistic Optimistic x 1
  6. They would spawn facing the direction opposite the target is facing.
     
  7. Will test it in a bit after I finish some homework and let you guys know what worked and what didn't
     
  8. You maybe want to use getEyeLocation for the target player istead of getLocation