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

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

1. ### TheNewTao

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

3. ### Dmg_Pro

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

4. ### 1Rogue

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 x 2
5. ### Janmm14

This does not work, here the teleported player will look in the opposite direction of the look direction of the player he should face.

6. ### Dmg_Pro

What? The player would teleport facing the target.. already tested it

• Optimistic x 1
7. ### 1Rogue

They would spawn facing the direction opposite the target is facing.

8. ### TheNewTao

Will test it in a bit after I finish some homework and let you guys know what worked and what didn't

9. ### Janmm14

You maybe want to use getEyeLocation for the target player istead of getLocation