Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Get the position where the player is looking at

Comments in 'Plugin Development' started by Svile, Apr 7, 2016.

  1. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    how can i get the block the player is looking at , having the yaw and pitch ? thanks :)
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    Entity::getDirectionVector()
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You are not really answering the question. How can you use getDirectionVector() to get the position?

    Entity::getDirectionVector() returns a normalized vector, which is the distance from the entity to one meter in front of the entity with respect to the entity's rotation. In other words, using $entity->add($entity->getDirectionVector()) returns one block in front of the player. However, you have to count it block by block, and sometimes you would miss some blocks.

    PocketMine has a class called BlockIterator, which can help in this case. However, the most straightforward way is to use Living::getTargetBlock(). You can use $Living->getTargetBlock($maxDist, $transparent) to get the block that the player is looking at. $maxDist is the maximum distance to search for - if there are no non-transparent blocks found within $maxDist at the player's direction, the player is considered as not looking at any blcoks, and the function will return null. $transparent is an array of block IDs in the keys, with arbitrary value. For example, you can pass [0 => true, 8 => true, 9 => true, 10 => true, 11 => true] as $transparent, so PocketMine will consider blocks with ID 0, 8, 9, 10 or 11 as transparent. If you don't pass the $transparent parameter, or pass an empty array, only air blocks are considered as transparent
    CraftYourBukkit and Svile like this.
  4. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    wonderful answer, thanks
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    When a question is interesting, it deserves more attention and more effort for answering. :) (unlike those that have been asked for tens of times on the forums; they don't help future readers at all because they are duplicated) (I think this is duplicated too, but I can't find the duplicated link)
    Svile and CraftYourBukkit like this.

Share This Page

Advertisement