Jump to content

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


Warven22

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(
  	player.level, 
  	player,
  	start,
  	start.add(addition),
  	player.getBoundingBox().expandTowards(player.getDeltaMovement()).inflate(1.0D),
	(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
Solved
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:

player.getBoundingBox().expandTowards(player.getDeltaMovement()).inflate(1.0D)

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.

getEntityHitResult
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

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Slot Bank Mandiri memberikan penawaran untuk pendaftaran baru akun pro dengan win rate 99% di Museumbola, Secara resmi Slot Mandiri memberikan secara khusus bagi member Museumbola untuk merasakan maxwin pada awal daftar sampai seumur hidup selama bermain di Museumbola. jadi mau kah anda mengambil kesempatan ini? segera klik DAFTAR untuk bergabung bersama Museumbola sekarang juga. ย 
    • Slot Bank BNI adalah pilihan tergacor untuk memulai bermain slot judi Online dimuka bumi saat ini. Jika anda mempunyai Bank BNI, Anda berkesempatan mendapatkan Akun Pro atau ID pro untuk bermain slot. Tunggu apa lagi? Segera daftarkan diri anda sekarang dan dapatkan kemewahan menang maxwin di Museumbola.
    • Halo para penggemar slot online! Apakah Anda mencari pengalaman bermain slot yang seru dan menguntungkan? Apakah Anda ingin menikmati slot gacor dari server Thailand sambil melakukan deposit melalui Mandiri dengan kesempatan meraih kemenangan besar? Anda telah sampai di tempat yang tepat! Kami di WINNING303 siap memberikan Anda pengalaman bermain yang mengasyikkan dan menguntungkan. Mengapa Memilih WINNING303? WINNING303 telah dikenal sebagai salah satu platform terbaik untuk bermain slot dengan berbagai keunggulan yang kami tawarkan kepada para pemain kami. Berikut adalah beberapa alasan mengapa Anda harus memilih WINNING303: Slot Gacor dari Server Thailand Kami menyajikan koleksi slot gacor terbaik dari server Thailand yang pastinya akan memberikan Anda pengalaman bermain yang menarik dan menguntungkan. Nikmati berbagai jenis permainan slot dengan tingkat kemenangan yang tinggi dan jackpot yang menarik. Deposit Mudah Melalui Mandiri Kami memahami pentingnya kemudahan dalam bertransaksi bagi para pemain kami. Oleh karena itu, kami menyediakan layanan deposit melalui bank Mandiri, salah satu bank terbesar di Indonesia. Proses depositnya cepat, mudah, dan aman, sehingga Anda dapat langsung memulai petualangan bermain tanpa hambatan. Peluang Maxwin Besar Di WINNING303, kami selalu memberikan peluang untuk meraih kemenangan besar. Dengan berbagai promosi dan bonus menarik yang kami sediakan, Anda memiliki kesempatan untuk memenangkan hadiah-hadiah yang fantastis dan meraih maxwin dalam bermain slot. ย 
    • SLOT Ratubet77 adalah bocoran slot gacor rekomendasi dari Ratubet77 yang bisa anda temukan di SLOT Ratubet77. Situs SLOT Ratubet77 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Ratubet77 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Ratubet77 merupakan SLOT Ratubet77 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Ratubet77. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Ratubet77 hari ini yang telah disediakan SLOT Ratubet77. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Ratubet77 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Ratubet77 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Ratubet77 di link SLOT Ratubet77. DAFTAR SEKARANG DAFTAR SEKARANG DAFTAR SEKARANG
    • I am currently running the 1.20.1 Occultcraft modpack in Curseforge and am having numerous troubles making a mob farm with the apotheosis mod. When trying to modify the stats of the spawners specific stats such as the range, spawn count, and max entities reset to default after every spawn. When the spawners spawn boss mobs with certain attributes, that I'm not sure of, the building it is in explode even with mob griefing turned off. This has happened multiple times with varying sizes for the explosions. I was wonder if there is any way to disable these explosions from happening or disable boss abilities with something in the game. I also wanted to know a fix for the resetting stats on spawners and why this is happening.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.