1.12.2 Moving armorstand like player

Discussion in 'Spigot Plugin Development' started by LetsHacks, Jan 24, 2020.

  1. Hello everyone,

    so what I want to do is, moving 4 armorstands around me like i am moving.
    If you would e.g. move forward, all 4 armorstands around me should move with me and still have the same distance as before.
    So that it always looks like this:
    http://prntscr.com/qsh0yb

    This should be a base for creating a vehicle using armorstands.

    Thanks for your help!
     
  2. drives_a_ford

    Moderator

    The simplest way is to listen to the PlayerMoveEvent and then move the armorstands in the same manner the player is moving.

    Location#subtract and then Location#toVector to get the travelled vector (from -> to in the event) and then add that to the location of each of the armorstands.
     
  3. Code (Java):
    stand.setVelocity(stand.getLocation().toVector().subtract(player.getLocation().toVector()));
    Should this work?
    Because, well, it unfortunately does not :(
     
  4. You can simply teleport the armor stand to where the player is standing. Teleporting moves the entity for small distances
     
  5. Well, teleporting would be fine, but then please tell me how to do that with 4 or more armorstands.
    Because I store all the armorstands in a list and set the velocity for each of them.
     
  6. Code (Java):
    List<ArmorStand> armorStands = ...
    armorStands.forEach(stand -> stand.teleport(player.getLocation()));
     
  7. Not all of them should be teleported to the location of the player.
    //EDIT actually none of them should be at the player's location. :)
    They still should have the same distance as in the screenshot at the top.
     
  8. @drives_a_ford

    So I now tried this:

    Code (Java):
     Vector vector = player.getVelocity();

                stand.getLocation().add(vector);
    and this:

    Code (Java):
    playerfrom.subtract(playerto);

                Vector vector = playerfrom.toVector();

                stand.getLocation().add(vector);
    But the armorstands do not move :(
     
  9. Ah, draw conclusions without reading, sry. Here's how:
    Code (Java):
    int dx = 1, dy = 0;
    for (ArmorStand armorStand: armorStandList) {
        final Location to = cosmeticPlayer.getLocation().add(dx, 0, dy);
        dy = dx;
        dx = dx < -1 ? 0 : dx-1;
        armorStand.teleport(to);
    }
    This solution (should) work only for only 4 armor stands thou
     
  10. drives_a_ford

    Moderator

    If you'd have read the javadocs of Entity#getLocation, you'd know it creates a copy of the location.

    You need to actually Entity#teleport

    All you really need to do (assuming the armorstands start off in the right position with respect to the player) is calculate the vector that the player moved, add that to each of the armostands' locations and the teleport them accordingly.
     
  11. @Schottky how would your method work with more than 4 armorstands?
    Furthermore, if it should be dynamic, with for example 100 armor stands?
     
  12. I‘m not home atm so I can only show you the direction. The dx and dy need to be adapted so that dx = sin(phi) * r and dy = cos(phi) * r, where phi is the angle between two adjacent armorstands (or 2 pi divided by the amount of stands, multiplied with the index of the current stand) and r is the radius; in your case I think that‘s one.