# Solved Armor stand rotate with the player

Discussion in 'Spigot Plugin Development' started by ArKeid0s, Jan 28, 2020.

1. ### ArKeid0s

Hello I have an armor stand spawning behind my player and following him but when my player roate the armor stand goes in front of him and does not stay behind (which is logical)
How can i solve that problem ?

2. ### jaylawl

1. get the player's yaw and add -180f
2. create a vector from that yaw (and pitch 0f)
3. location l = p.getLocation().clone().shift(vector you created here)
4. teleport armor stand to l

if you want the armor stand to be a certain distance behind the player, you do

vector.multiply(x)

x being the distance you want between the player and the armor stand in blocks

if you dont add the -180f in the first step, the armor stand will appear infront of the player instead

note: you may have to add another 90f to the yaw to correct for "notch-math"

3. ### ArKeid0s

.shift(vector) does not exist :/

4. ### ArKeid0s

Code (Java):
public void walkToLocation(ArmorStand as,Player player) {
float pYaw = player.getLocation().getYaw() + (-180f);
float pPitch = 0f;
Vector pYawVect = new Vector(pYaw, pPitch, 0f);
pYawVect.multiply(2);
Location l = player.getLocation().clone().setDirection(pYawVect);
as.teleport(l);
}
the rotation is working but the multiply does not make the armorstand stay 2 blocks behind the player

5. ### Schottky

How is that even working...
You need to set the Direction Vector, not a Vector that contains the direction.
you can compute the x-value using
Code (Java):
And the z-value using
Code (Java):

6. ### ArKeid0s

like that ?
Code (Java):
public void walkToLocation(ArmorStand as,Player player) {
Location loc = player.getLocation();
double pX = loc.getX();
double pY = loc.getY() + 1;
double pZ = loc.getZ();
Location locTp = new Location(player.getWorld(), pX, pY, pZ, loc.getYaw(), loc.getPitch());
float pYaw = locTp.getYaw() + (-180f);
Vector pYawVect = new Vector(xVect, 0f, zVect);
pYawVect.normalize().multiply(2);
Location l = locTp.clone().setDirection(pYawVect);
as.teleport(l);
}
and how can i make the armorstand always face the player ?

#6
Last edited: Jan 29, 2020
7. ### Schottky

Yes. Since the absolute value of e^(2*j*x) is one for all values x and can be written as the sin(x) + cos(x), the absolute value of the direction vector is also always one and thus doesn‘t have to me normalized (saves you one square-root)

8. ### ArKeid0s

Okay but when i multiply my vector the armorstand does not stand back by 2 blocks why ?

9. ### Schottky

Because you only set the direction; which only defines where it‘s looking at. You‘ll also need to set the x and z. You can accomplish this by adding your pYawVec to your tp-vector

10. ### ArKeid0s

Hmm two more things, the armor stand rotate at the opposite of my player when i look right the armor stand rotate to the right instead of left to stay behind the head of the player :/ and how can i make my armor stand always look at the player ?

Thanks a lot for your help so far

11. ### ArKeid0s

here is a video to illustrate the rotation

12. ### Schottky

Just change the sign of either x or z (don‘t know exactly which one)

13. ### ArKeid0s

Thanks a lot for your help it is working perfectly

14. ### Schottky

Can you also tell whether you have to invert x or z so it works?

15. ### ArKeid0s

Code (Java):
public void walkToLocation(ArmorStand as,Player player) {
Location loc = player.getLocation();
double pX = loc.getX();
double pY = loc.getY() + 1;
double pZ = loc.getZ();
Location locTp = new Location(player.getWorld(), pX, pY, pZ, loc.getYaw(), loc.getPitch());
float pYaw = locTp.getYaw();