# How can i get the position behind a player

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

1. ### DommiHD

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 x 1
2. ### Yamakaja

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. ### DommiHD

i hate sin and cos ^^ ;D but i will try it

4. ### xSkill_Cycanx

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();
x.subtract(pl);
x.multiply(-1D);
EDIT:
Code (Text):

• Agree x 1
5. ### stonar96

I am curious why you would need sin and cos for that?

6. ### Yamakaja

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. ### Squallz

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. ### Yamakaja

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. ### xSkill_Cycanx

You should try Comment #4 its fastest method in your case.

10. ### Yamakaja

No its not, its actually overly complicated. The same thing can be accomplished by that code:

Code (Text):

#10
Last edited: May 10, 2016

12. ### Yamakaja

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. ### Squallz

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 = c.dot(d);
if (delta > 0) {
if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName() == null) {
return;
}
//they are behind the player

14. ### Yamakaja

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. ### Squallz

The person they are arresting....

17. ### Squallz

My point was you questioned how I did it. Thats how.

18. ### Yamakaja

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

• Friendly x 1
19. ### stonar96

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;