How do I detect if the player crosshair is looking at my entity?

Started by PIGLIN98 on

Topic category: Help with Minecraft modding (Java Edition)

Joined Oct 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How do I detect if the player crosshair is looking at my entity?
Thu, 04/24/2025 - 23:11 (edited)

I dont really have idea on how to do it. I never seen procedure blocks to even work with crosshairs in general. My goal is to just detect if the crosshair is looking directly at the entity

Edited by PIGLIN98 on Thu, 04/24/2025 - 23:11
Joined Apr 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
there are raytracing blocks,…
Sun, 06/01/2025 - 11:09

there are raytracing blocks, you can combine with "does entity exist at xyz". 
you can use a while loop at player tick update, with a local number variable, that starts with 1, and keeps increasing by 1, with every repeat, untill the variable reaches the maximum range you set. 

 the loop checks: does entity exist at (look xyz position of eventtargetentity with raytrace distance of "local variable") in square cube size 1 of type "living entity". 
If yes, set another local entity variable to store the entity for further use, then break out of loop,
if no, check if block at that location is solid, if yes, break out of loop, if that is also false, increase local number variable by 1. 

then if local entity variable is living entity, do whatever the procedure is supposed to do.