How can i get the position behind a player

Discussion in 'Spigot Plugin Development' started by DommiHD, May 10, 2016.

  1. I wanted to teleport an Armorstand 3 blocks behind a player every second i already made a scheduler i just need to know how i can get the position behind the player
    • Funny Funny x 1
  2. Oookay, soo. First off you will want to get the players yaw: player.getLocation().getYaw()

    Now use sin / cos to construct a vector from that and multiply it by 3, done. (Assuming you know how that stuff works)

    EDIT: Actually you might have to multiply it by -3
  3. i hate sin and cos ^^ ;D but i will try it
  4. I using this

    Code (Text):
    HashSet<Material> transparent = new HashSet<Material>();
    Player p = ...
    Block block = p.getTargetBlock(transparent, 3);
    Location loc = block.getLocation();
    Vector x = loc.toVector();
    Vector pl = p.getLocation().toVector();
    Location finalLoc = p.getLocation().add(x);
    Code (Text):
    • Agree Agree x 1
  5. I am curious why you would need sin and cos for that?
  6. Yaw:
    Since Yaw is an angle you will have to use trigonometry to convert that to a vector, but if all OP wanted is spawning the Armor Stand in the opposite direction of where the player is looking see my post above. EG above the player if he's looking down
  7. You don't.

    I coded something like this before and I just used vectors. I used this so Police could only arrest people from behind. (if the player did not have line of sight)
  8. What happens when players look down? You can't set the Y value of the vector to 0 because then you'd have a 0 Vector ...
  9. You should try Comment #4 its fastest method in your case.
  10. No its not, its actually overly complicated. The same thing can be accomplished by that code:

    Code (Text):
    Location loc = player.getLocation().add(player.getLocation().getDirection().normalize().multiply(-3d));
    #10 Yamakaja, May 10, 2016
    Last edited: May 10, 2016
  11. What about Location#getDirection()?
  12. Doesn't get you the players rotation if he's looking down or up

    Edit: Also you'd have to deal with things spawning in the ground or too close to the player
  13. Code (Text):
    Location lo = pl.getEyeLocation();
                Location o;
                if (en instanceof Player) { //En is the other entity
                    o = en.getEyeLocation();
                } else {
                    o = en.getLocation();
                final Vector c = lo.toVector().subtract(o.toVector()); // Get vector between you and other
                final Vector d = pl.getEyeLocation().getDirection(); // Get direction you are looking at
                final double delta =;
                if (delta > 0) {
                    if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName() == null) {
                   //they are behind the player
  14. What other entity?

    Edit: Oh thats your plugin ... so yeah. What you're doing here is getting the dot product between the vector that the player should be looking and where he is looking. But that wasn't the question.

    OP wants to spawn something behind a player, there is no other entity invloved ...
  15. The person they are arresting....
  17. My point was you questioned how I did it. Thats how.
  18. Oh, thats a missunderstanding right there: I meant to say that that method wont work in OP's scenario where you want to spawn something behind the player, and optimally on the same level as them. What what you did its perfectly fine, although i think i would have shortened it a little bit :p
    • Friendly Friendly x 1
  19. Oh I see, there would also be problems if you look up or down.

    I think that's it:
    Code (Text):
    Location location = player.getLocation();
    double distance = 3.0d;
    double yawRadians = Math.PI * location.getYaw() / 180;
    location.add(distance * Math.sin(yawRadians), 0, - distance * Math.cos(yawRadians));
  20. Pretty sure you could check which direction they are facing, (WEST, EAST, NORTH, SOUTH) and then use
    Code (Text):
    Location spawnLoc = player.getLocation().add(-3,0,0); //facing one way

    Location spawnLoc = player.getLocation().add(0,0,-3); //facing other way