Started by
ConRam
on
Topic category: Help with modding (Java Edition)
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)
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.
holy, is that good lol, thanks man :D
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?
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.
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
Yup, just find the part where it sets 'y1' to the player position, and add 1.5 to it.
thanks for all your help its working great :D
You are welcome.