Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

(SOLVED) [1.17.1] Getting Entity That Player Is Pointing At


Recommended Posts

I need to know how to get the mob a player is pointing at from some distance (not just regular reach distance). I looked around quite a lot in both Google and source code, but I find myself stumped, confused, and unsure.

I attempted this:

public static EntityHitResult getTargetedEntity(Player player) {
  Vec3 start = player.getEyePosition();
  Vec3 addition = player.getLookAngle().multiply(new Vec3(10000D, 10000D, 10000D));
  return ProjectileUtil.getEntityHitResult(
	(val) -> true);

It seems to work, but the range is very short regardless of the number I multiply with.

Am I misunderstanding this function? Is there a better way to get the entity the player is pointing at?

Edited by Warven22
Link to comment
Share on other sites

3 hours ago, poopoodice said:

The problem is it's only gathering the entities that are inside/intersects with the bb:


So make the bounding box bigger.

I've tried expandingTowards using

player.getLookAngle().multiply(new Vec3(10000D, 10000D, 10000D));

rather than the player's movement (since movement can be 0), along with an inflate of a similarly large number. My range seems to have increased, but not as expected, as if I'm not visualizing how these changes to the bounding box actually affects anything. Can I set up this bounding box to be drawn in the debug elsewhere?

I feel like I do not understand how the ProjectileUtil works. I don't like the idea that I'm "shooting" the player towards a point, but it seems to be the only way.

Is there no other way for this to be done? No raycast function? No better way all the other modders use?

Link to comment
Share on other sites

8 hours ago, Luis_ST said:

did you read poopoodice post? since expanding the LookAngle != BoundingBox

I should have made it more clear that I did try that solution and what I actually did:

Vec3 start = player.getEyePosition();
Vec3 addition = player.getLookAngle().multiply(new Vec3(1000000D, 1000000D, 1000000D));
return ProjectileUtil.getEntityHitResult(
  player.level, player,
  start, start.add(addition),
  player.getBoundingBox().expandTowards(addition).inflate(1000000D), (val) -> true);

It didn't seem to work, as the range doesn't seem as big as it should be, needing me to be a few blocks (around 10) away rather than anything in view.

Edited by Warven22
clarification, less snarky wording
Link to comment
Share on other sites

  • Warven22 changed the title to (SOLVED) [1.17.1] Getting Entity That Player Is Pointing At

Please forgive this additional bump, but I want to assist future google visitors by sharing my solution.

I have found a solution by using a combination of two different functions: ProjectileUtil.getEntityHitResult & Item.getPlayerPOVHitResult.

doesn't take into account player's rotation. getPlayerPOVHitResult is for blocks, but takes into account player's rotation. So by using the positions from getPlayerPOVHitResult, creating a bounding box from it, and using the loop for entities in getEntityHitResult, it creates a raycast function that fires from the player's eyes to where they are pointing.

I'm not sure if any of that is right, but one thing I'm sure of is it works. If you increase the range to a high number, you can detect an entity quite far away.

public static EntityHitResult getPlayerPOVHitResult(Player player) {
  float playerRotX = player.getXRot();
  float playerRotY = player.getYRot();
  Vec3 startPos = player.getEyePosition();
  float f2 = Mth.cos(-playerRotY * ((float)Math.PI / 180F) - (float)Math.PI);
  float f3 = Mth.sin(-playerRotY * ((float)Math.PI / 180F) - (float)Math.PI);
  float f4 = -Mth.cos(-playerRotX * ((float)Math.PI / 180F));
  float additionY = Mth.sin(-playerRotX * ((float)Math.PI / 180F));
  float additionX = f3 * f4;
  float additionZ = f2 * f4;
  double d0 = ZapEffect.RANGE;
  Vec3 endVec = startPos.add((double)additionX * d0, (double)additionY * d0, (double)additionZ * d0);
  AABB startEndBox = new AABB(startPos, endVec);
  Entity entity = null;
  for(Entity entity1 : player.level.getEntities(player, startEndBox, (val) -> true)) {
	AABB aabb = entity1.getBoundingBox().inflate(entity1.getPickRadius());
	Optional<Vec3> optional = aabb.clip(startPos, endVec);
    if (aabb.contains(startPos)) {
      if (d0 >= 0.0D) {
        entity = entity1;
        startPos = optional.orElse(startPos);
        d0 = 0.0D;
    } else if (optional.isPresent()) {
      Vec3 vec31 = optional.get();
      double d1 = startPos.distanceToSqr(vec31);
      if (d1 < d0 || d0 == 0.0D) {
        if (entity1.getRootVehicle() == player.getRootVehicle() && !entity1.canRiderInteract()) {
          if (d0 == 0.0D) {
            entity = entity1;
            startPos = vec31;
        } else {
          entity = entity1;
          startPos = vec31;
          d0 = d1;

  return (entity == null) ? null:new EntityHitResult(entity);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • It wasn't supposed to, just a hint if he scans for leaves:  
    • Looks like you changed the java executable path to javaw or something else. Do not do this. If you change it, you must point it at the java executable.
    • 18:28:57.216 launcher main Preparing to launch minecraft client for 1.18.1-forge-39.0.59 18:28:57.568 launcher main Couldn't fetch SHA1 checksum for http://s3.amazonaws.com/Minecraft.Download/indexes/legacy.json 18:28:57.570 launcher main Have local file C:\Users\Edvinas\AppData\Roaming\.minecraft\assets\indexes\legacy.json but don't know what size or hash it should be. Have to assume it's good. 18:28:58.661 launcher main Couldn't fetch SHA1 checksum for https://s3.amazonaws.com/Minecraft.Download/versions/1.18.1-forge-39.0.59/1.18.1-forge-39.0.59.jar 18:28:58.664 launcher main Have local file C:\Users\Edvinas\AppData\Roaming\.minecraft\versions\1.18.1-forge-39.0.59\1.18.1-forge-39.0.59.jar but don't know what size or hash it should be. Have to assume it's good. 18:28:58.665 launcher main Minecraft client 1.18.1-forge-39.0.59 is ready to start. 18:28:58.665 launcher main Starting! 18:28:58.851 launcher main Version does not support log configuration, will assume one plaintext entry per line 18:29:04.042 game Usage: javaw [-options] class [args...] 18:29:04.042 game (to execute a class) 18:29:04.042 game or javaw [-options] -jar jarfile [args...] 18:29:04.042 game (to execute a jar file) 18:29:04.042 game where options include: 18:29:04.042 game -d32 use a 32-bit data model if available 18:29:04.042 game -d64 use a 64-bit data model if available 18:29:04.042 game -server to select the "server" VM 18:29:04.042 game The default VM is server. 18:29:04.042 game 18:29:04.042 game -cp <class search path of directories and zip/jar files> 18:29:04.042 game -classpath <class search path of directories and zip/jar files> 18:29:04.042 game A ; separated list of directories, JAR archives, 18:29:04.042 game and ZIP archives to search for class files.  
    • Seems to be a bug with infinitylib. Make sure it is up to date. If already is, report this bug to the mod author.
    • I play Minecraft using MultiMC, and am currently playing 1.16.5. I don't use modpacks; Instead, I install all my mods one by one. After a certain number of mods downloaded this way, problems obviously begin to happen (usually after around 110 mods. 100 mods has been fine but idk if its mod conflicts or the number of mods causing a problem). Here is the crash report: https://pastebin.com/pidPDt5g Just realized after submitting topic that the debug.log contains the crash report in it. Didn't think to remove this before posting. Here is the debug.log report: https://gist.github.com/trevorreynen/b4156112bfa4542de1319bcc294fa97b NOTE: The debug.log is extremely large, my crash is at the bottom. Time should say 10:41 Extra info that may be helpful: I get 60-100 fps with all the mods listed in the pastebin crash report. Most noticeable problem, either a freeze or an fps drop (which doesn't show dropping so probably a freeze) after roaming while moving faster than walking. This is most likely the problem and the reason I crashed. However, I have a different instance of Minecraft with ~30 mods less. That instance also has this problem but not nearly as much or as bad of a freeze. I have not tried loading this pack without Optifine. I am willing to try without it but I am assuming the only reason I am getting good fps is because of Optifine so I'm skeptical in removing it. My personal modded resource pack combining many mod textures from PureBDCraft also includes textures for mods I don't have installed. Not sure if that could cause major problems or if it's just skipped over and doesn't matter. Idk if it's my computer and I need more resource dedication, mod conflicts, or something else I am not thinking of. If you manage to find why I crashed, how? What should I be looking at for common crash problems?
  • Topics

  • Who's Online (See full list)

  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.