Solved Getting LivingEntity in Line of Sight

Discussion in 'Plugin Development' started by Sulphate, Dec 20, 2016.

Thread Status:
Not open for further replies.
  1. Hello,

    I'm trying to get the LivingEntity the player is looking at (if any), and I've gotten this far:
    private boolean isLookingAt(Player player, LivingEntity entity) {
           Location eye = player.getEyeLocation();
           Vector toEntity = entity.getEyeLocation().toVector().subtract(eye.toVector());
           double dot = toEntity.normalize().dot(eye.getDirection());
           return dot > 0.99D;
    However I cannot figure out how I can get if they are looking at anything apart from the eyes?

  2. Offline


    1. Create a location. This will be the "checker".
    2. Create a Vector. This is the player's line of sight. Normalize this and divide it by 2.
    3. Create a for-int loop. I would recommend looping 200 times (100 *2)
    4. Every time it loops, add the vector to the checker.
    5. Using the checker. loop through the entities nearby. If there is an entity within 0.5 (most reliable, may be less accurate. To be more accurate, lower the number) of the checker, that is the entity the player is looking at.
  3. @Zombie_Striker

    I have written this code, but when it broadcasts the player's location and the final checked location, they seem to be the same, as you can see here.

    I can't for the life of me figure out why. Any help appreciated.

    @Zombie_Striker ? ;-;
    Last edited: Dec 21, 2016
  4. Offline



    You should definitely rewrite that loop, it's kind of a mess. The actual issue is that you're using continue too liberally. Only use it if you really mean to skip the rest of the loop body for that iteration, to just continue executing, use nothing.

    You're skipping the statement that updates the variable "base."
  5. Thank you so much! I knew I was missing something obvious. Don't worry, I always just get it done first, then make it better. Thank you!
  6. Offline


    If your problem has been solved, mark this thread as solved.
Thread Status:
Not open for further replies.

Share This Page