Last active
April 25, 2020 15:06
-
-
Save yuyaprgrm/ed4c1ce2eb49974a9a113fe5e9a9c787 to your computer and use it in GitHub Desktop.
PocketMine-MP function for getting entity player is looking at.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function getEntityLookingAt(Player $player):?Entity{ | |
| $k = 0.5; //Const for threshold | |
| $level = $player->getLevel(); | |
| $playerSightDirectionNormalizedVector = $player->getDirectionVector(); | |
| $targetEntity = null; | |
| foreach ($level->getEntities() as $entity){ | |
| if($entity === $player)continue; | |
| $playerToEntityVector = $entity->subtract($player); | |
| $dot = $playerToEntityVector->dot($playerSightDirectionNormalizedVector); | |
| $distance = $playerToEntityVector->length(); | |
| $comparison = ($distance-$dot)*($distance); | |
| if($comparison < $k){ | |
| return $entity; | |
| } | |
| } | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment