Skip to content

Instantly share code, notes, and snippets.

@yuyaprgrm
Last active April 25, 2020 15:06
Show Gist options
  • Select an option

  • Save yuyaprgrm/ed4c1ce2eb49974a9a113fe5e9a9c787 to your computer and use it in GitHub Desktop.

Select an option

Save yuyaprgrm/ed4c1ce2eb49974a9a113fe5e9a9c787 to your computer and use it in GitHub Desktop.
PocketMine-MP function for getting entity player is looking at.
<?
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