Solved Teleporting the player 1-2 blocks behind an entity based on location

Discussion in 'Plugin Development' started by Ward1246, Jun 27, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Ward1246

    Hello! I tried to be as specific with my title as possible. I am trying to teleport the player 2 blocks behind an entity based on where the player and the entity are located, in a way swap what side you are on of the entity. I was able to do the first part, teleporting the player to the opposite side of the entity, my question is how can I make it a fixed distance of 1 or 2 blocks (depending on how far back I want to be). My code for the teleporting to the opposite side of the entity:
    Code:
    //player being a player variable, and e being the entity variable of course
    Vector v = player.getLocation().toVector().subtract(e.getLocation().toVector());
                        player.teleport(e.getLocation().add(v.multiply(-1)));
                        player.teleport(player.getLocation().add(0, e.getLocation().getY() - player.getLocation().getY(), 0)); // I also wanted the entity and player on the same Y level
    If the player is 3 blocks away when using the above code, they will be teleported 3 blocks away on the opposite side of the entity, I want to make this a fixed distance of 1 or 2 blocks at any distance. Any help is much appreciated!
     
  2. Offline

    Zombie_Striker

    @Ward1246
    normalize the vector and then multiply it by 2. This should make sure it will always two blocks away.
     
  3. Offline

    Ward1246

    Thank you very much! That worked like a charm! I thought the solution would be similar, I forgot about normalizing the vector though. Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page