Solved Armor stand rotate with the player

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

  1. 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
     
  2. 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. .shift(vector) does not exist :/
     
  4. 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. 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));
     
  6. 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 ?
     
    #6 ArKeid0s, Jan 29, 2020
    Last edited: Jan 29, 2020
  7. 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. Okay but when i multiply my vector the armorstand does not stand back by 2 blocks why ?
     
  9. 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. 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. here is a video to illustrate the rotation

     
  12. Just change the sign of either x or z (don‘t know exactly which one)
     
  13. Thanks a lot for your help it is working perfectly
     
  14. Can you also tell whether you have to invert x or z so it works?
     
  15. 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 ;)
     
    • Like Like x 1