Search code examples
minecraftbukkit

How to teleport a player two block on his left?


I tried several things, like using vectors, but It didn't work for me. Than I tried searching on the internet and it didn't work as well.

Vector direc = l.getDirection().normalize();
direc.setY(l.getY());
direc.normalize();
direc.multiply(-1);
l.add(direc);

Player#teleport(l.getBlock().getLocation());
// or
Player#teleport(l);

Solution

  • Location location = player.getLocation();
    Vector direction = location.getDirection();
    direction.normalize();
    
    float newZ = (float)(location.getZ() + (2 *  Math.sin(Math.toRadians(location.getYaw() + 90 * direction)))); //2 is your block amount in Z direction
     
    float newX = (float)(location.getX() + (Math.cos(Math.toRadians(location.getYaw() + 90 * direction))));
    

    You have to know in which direction you want to teleport the player