Solved Making a player directly face another player

Discussion in 'Plugin Development' started by Xp10d3, Apr 29, 2021.

  1. Offline

    Xp10d3

    How would I make the player face the other player? I'm using SchematicAPI to paste a schematic, however it always faces west. I want one cage to face east instead of west. On top of that, both players face west instead of one facing east. I tried setYaw, but it doesn't work for SchematicAPI.
    Code:
    rXTra3Phgy | SourceBin
     
  2. Offline

    KarimAKL

    @Xp10d3 You can make a player face another player by setting their direction and teleporting them.
    Code:Java
    1. Vector direction = player2.getLocation().getVector().subtract(player1.getLocation().getVector());
    2.  
    3. Location location = player1.getLocation();
    4. location.setDirection(direction);
    5.  
    6. player1.teleport(location);

    I believe it should look something like this. However, this is written by hand.
     
  3. Offline

    Xp10d3

    Thanks! Yep; that works. What if I want to face a certain direction? Ex. north, east, west, south, etc.
     
  4. Offline

    KarimAKL

    Unfortunately, I do not believe there is any API for that, but this should work:
    Code:Java
    1. float yaw = player.getLocation().getYaw();
    2. BlockFace faces[] = { BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH, BlockFace.EAST };
    3.  
    4. BlockFace face = faces[Math.round(yaw / 90) % 4];
    5.  
    6. player.sendMessage("You are facing: " + face.name());
     
  5. Offline

    Xp10d3

    Thanks :) But how would I make the player face that location? I don't believe I can set the player's direction based on BlockFace.
     
  6. Offline

    KarimAKL

    @Xp10d3 Oh, that is my bad. I misunderstood what you meant.

    You can reverse it to get your desired result.
    Code:Java
    1. List<BlockFace> faces = Arrays.asList(BlockFace.SOUTH, BlockFace.WEST, BlockFace.NORTH, BlockFace.EAST);
    2.  
    3. BlockFace face = BlockFace.NORTH; // This is the direction you want to face
    4. int index = faces.indexOf(face);
    5.  
    6. Location location = player.getLocation();
    7. location.setYaw(index * 90);
    8.  
    9. player.teleport(location);
    10. player.sendMessage("You are now facing: " + face.name());
     
    Xp10d3 likes this.
  7. Offline

    Xp10d3

    Thanks! That's really helpful. I'll try it out
     
  8. Offline

    Xp10d3

    Sorry for not replying lol. Been quite busy. Works perfectly! Thanks :D
     
    KarimAKL likes this.

Share This Page