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 ? Thanks in advance ArKeid0s
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"
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
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): Math.sin(Math.toRadians(yaw)); And the z-value using Code (Java): Math.cos(Math.toRadians(yaw));
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); double xVect = Math.sin(Math.toRadians(pYaw)); double zVect = Math.cos(Math.toRadians(pYaw)); 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 ?
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)
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
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
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(); double xVect = Math.sin(Math.toRadians(pYaw)); double zVect = Math.cos(Math.toRadians(pYaw)); Vector pYawVect = new Vector(xVect, 0f, -zVect); locTp.add(pYawVect); pYawVect.multiply(3); EulerAngle angle = new EulerAngle(0, 135, 0); Location l = locTp.clone().setDirection(pYawVect); as.setHeadPose(angle); as.setBodyPose(angle); as.setRightArmPose(angle); as.teleport(l); } here is my final class