How to make a player's head turn in the direction of an entity?

Discussion in 'Plugin Development' started by MrShnig, Nov 10, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    MrShnig

    Hello there!
    I would like to know how to make a player's head automatically face the nearest entity, or whenever an entity walks in range the player's head turns to face the entity (in 1.9.2 bukkit/spigot).
    (I'm using .getNearbyEntities(x, x, x).)
    Thanks for the help!
     
  2. @MrShnig

    player.getLocation().setYaw(newYaw) should set the yaw of the player. The yaw determines which direction the player is facing.
    You will need gotrinometrics (or however I am supposed to spell that) to determine the right yaw.
     
    MrShnig likes this.
  3. Offline

    MattTheBeast

    @MrShnig
    First you need to find the closest entity so just compare the distances. Use the squared distance if you want better performance.

    Then you need to find the direction the player should be facing.
    An easy way to do that would be to make a simple method that takes in two locations and returns a vector.

    Finally you need to teleport the player to the location with the correct yaw and pitch.
    @knokko player.getLocation().setYaw() will not work, the player needs to be teleported
     
    MrShnig likes this.
  4. Offline

    MrShnig

    Doesn't work. Tried and it teleports me way off, doesn't even return an error.
    Code:
                                       Vector vec = player.getLocation().toVector().subtract(entity.getLocation().toVector());
    
                                       Location loc = vec.toLocation(player.getWorld());
                                       player.teleport(loc);
     
  5. Offline

    Zombie_Striker

    @MrShnig
    1. First, By getting the Player's location and subtracting the Entity's location, you're creating a vector in the opposite direction.
    2. The vector is meant to be used to set the direction the player is looking, not to be converted back to a location. First set loc to the player's location, and then use setDirection instead of toLocation.
     
    MrShnig likes this.
  6. Offline

    MrShnig

    Ok so I solved that. But what if I wanted to move the player's head back to the original position right after?
    Code:
                                       Vector vec = player.getLocation().toVector().subtract(entity.getLocation().toVector());
                                       Location loc = player.getEyeLocation().setDirection(vec);
                                      
                                       loc.setX(player.getLocation().getX()); loc.setY(player.getLocation().getY()); loc.setZ(player.getLocation().getZ());
    
                                       player.teleport(loc);
    I tried doing it already, but it didn't work out so I removed the code. Here's how I got the player to look at the nearest entity though.
    Another problem is that I want the player to face the nearest entity, but if I don't set loc's x, y & z to the player's, then it teleports the player whenever there's an entity in range repeatedly until the player is out of the range. Anything to offer for that?
     
  7. Offline

    Zombie_Striker

    You don't need to set the player's location if you use player.getLocation() from the start.

    If you want to reset the player's head position after some time, store the original direction somewhere, and use a BukkitRunnable to wait the amount of time before setting the direction back to what it was.
     
Thread Status:
Not open for further replies.

Share This Page