Check if player is looking at an entity?

Discussion in 'Plugin Development' started by thebigdolphin1, Dec 21, 2012.

Thread Status:
Not open for further replies.
  1. I dont have any code, but I need to check if the player is looking/facing towards a player/entity.
    Basicly like .hasLineOfSight(), but with what they can see, not whats rendered/downloaded.
     
  2. Offline

    fireblast709

    You can calculate the direction the player is looking at using yaw and pitch. Then it would be a matter of checking nearby entities, checking if they are in the line of sight (considering distance aswell, as an entity does not have to be on the center of the line if it is close)
     
  3. Offline

    fireblast709

    You could also use these 2 Vector objects:
    Code:
    Vector direction = player.getLocation().getDirection();
    // And
    Vector directionToEntity = player.getLocation().substract(otherentity.getLocation()).toVector();
     
  4. fireblast709
    I dont really understand vectors and all that, can you give me some code to detect like a distance in an int/double for how different looking at the centre of the mob. I said that wrong somewhere.
    Basicly, hitting the mob while looking at the middle of it returns a 0 (if it was exactly in the middle), nextto it could be 0.01 or something
    Thanks! :D
     
  5. Offline

    fireblast709

    thebigdolphin1 distance would just be location.distance(otherlocation)
     
  6. fireblast709
    I dont want distance, but direction.
    So I can get how "off" a player is by the way they're facing (not where hit or whatever)
     
Thread Status:
Not open for further replies.

Share This Page