1.12.2 Spawn Entity 2 blocks in front of the Player

Discussion in 'Spigot Plugin Development' started by jd007, Jul 13, 2021.

  1. Hello,
    How can I make an Entity spawns 2 blocks in front of the player? I can't use things like geteyelocation because then, the mob spawns underground (in case you are looking to the floor) / Above you (In case you are looking to the sky).
  2. You can actually in fact use the eye location to get a direction,then simply add 2 blocks using the direction then simply add it on top of the player location and done,you should have a location 2 blocks in front of the player,however the spawning in floor things its up to you to do the checking(i guess dont modify Y at all?)
    • Like Like x 1
  3. How should it be? I am new to coding.
  4. Is there a solution? I really need it.
  5. Hello jd007!

    You can do the following to get 2 blocks ahead.

    Code (Java):
    Player player = //player
    Location currentLoc = player.getLocation();
    Vector vec = player.getEyeLocation().getDirection().multiply(2);
    //gets the player direction vector, then normalize to have a length of 1(unit vector), then multiple to have a length of 2.

    Location newLocation = currentLoc.add(vec); //gets the new position
    then from there you get the world, and spawn the entity at newLocation.

    EDIT: removed .normalize() after .getDirection(). Thank you wand555 for pointing out that it is already normalized :D
    #5 Potatoes_Duck, Jul 17, 2021
    Last edited: Jul 18, 2021
    • Like Like x 1
    • Agree Agree x 1
  6. That's correct, but a small note: getDirection() is already normalized
    • Like Like x 1