RayTracing

Started by ConRam on

Topic category: Help with modding (Java Edition)

Last seen on 22:07, 19. Aug 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
RayTracing

hey i'm trying to make a system to detect and do damage to an entity when looking at the entity (mob or player), any ideas on how to do this especially with no real range limits (aside render distance). i also want to make sure it could possibly go through a block / wall (only one layer thick) but only certain blocks (like only go through dirt). if you have any ideas let me know i've been doing some messing around with the raytracing to find where the player is looking but can't seem to get that to work with dealing damage.

 

(Bonus: if you know a way to show visually where the player is looking that could be cool)

Last seen on 22:49, 8. Aug 2024
Joined Jul 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Here's a simple raycast…
Tue, 07/30/2024 - 01:22

Here's a simple raycast procedure that can detect entities through blocks: https://file.io/LPfjx31t6Vuk

You should be able to easily adjust it to suit your needs.

If you want to test it before using it, I would set the global trigger to "Player right clicks with item" and see what the procedure does.

Last seen on 22:07, 19. Aug 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
holy, is that good lol,…
Tue, 07/30/2024 - 18:01

holy, is that good lol, thanks man :D

Last seen on 22:07, 19. Aug 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
quick question about it how…
Tue, 07/30/2024 - 18:17

quick question about it how do you think i should stop it going throw certain blocks but still letting it go through others then along with a variable saying how many blocks it can go through?

Last seen on 22:49, 8. Aug 2024
Joined Jul 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I think this'll work, haven…
Tue, 07/30/2024 - 19:30

I think this'll work, haven't tested it: https://imgur.com/a/dsEeA1h

You should be able to add the parts in the red box, but you'll have to make a local variable in place of the "counter" variable that I'm using.

The top 'if' block checks if there's a dirt or stone block at the current point on the line, and if there is, the line stops there (you can specify whatever blocks you want the line to stop at). Otherwise, if there is a non-air block, increase the counter by 1.

The bottom "if" block checks if the counter equals the number in the yellow circle, and if it does, the line stops there. You can set that number to the max amount of blocks that the line can go through.

Keep in mind that at certain angles, it may count the same block twice, so the raycast may not always go through the same number of blocks that you have set. There are workarounds for this issue, but I have not included them here.

Last seen on 22:07, 19. Aug 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
its working well, the only…
Tue, 07/30/2024 - 20:41

its working well, the only thing is, is there an easy way to move the source of the raytrace from your feet to the chest ish just so the raytrace doesn't clip the ground which i've noticed it does once in a while

Last seen on 22:49, 8. Aug 2024
Joined Jul 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yup, just find the part…
Tue, 07/30/2024 - 21:34

Yup, just find the part where it sets 'y1' to the player position, and add 1.5 to it.

Last seen on 22:07, 19. Aug 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
thanks for all your help its…
Wed, 07/31/2024 - 01:13

thanks for all your help its working great :D

Last seen on 22:49, 8. Aug 2024
Joined Jul 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You are welcome.
Wed, 07/31/2024 - 01:29

You are welcome.