For my game, I have a knife that you can throw/slash to kill other players. But I feel as though it isn’t all that well programmed. [I didn’t program it as math isn’t my strong suite.] It used touched which I already know is awful, and it’s completely handled by the server and many times it’s hard to tell where the knife is when it’s thrown or if it even hit someone or not. Could someone point me in the right direction to make a better one myself? I’ve read that raycasting could be good, but I’m unsure if that would only apply to hitscan weapons…?
Please can you clarify the question. At the moment, it’s difficult to tell what you’re asking. Do you want a redesigned knife mechanism? Do you want us to review your code? Do you want to know about hitscans? Please reply/edit your original post to be more clear and specific. However, I will have a go at answering a few questions.
Let’s deal with one thing at a time.
That isn’t necessarily true. There are circumstances where “touched” is a good solution to detecting a hit, but this isn’t one of them (in my opinion, anyway). Touched can be unreliable, especially with very fast moving objects, and you don’t want your players to hit each other and not have it register. Hitscans (raycasts, to be specific) would be far better in this circumstance. Not to mention, if the player throws a lot of knives, you’re going to have to be disconnecting and reconnecting events a lot. No, raycasting is better here.
If you want information on raycasting, please read this wiki example: Documentation - Roblox Creator Hub
No, it doesn’t only apply to “hitscan weapons”. By that, I assume you mean guns, lasers etc.
With a bit of a redesign, you can make your raycasts work for most things.
In this instance, you could raycast from a player’s torso or the tip of the knife to detect a hit. If the raycast hits, then you detect the knife has hit a player.
When detecting throw hits, simply raycast from the arm (or some other suitable location) and see, once again, if the raycast has hit an object. However, change the range of the raycast so that it can continue for a suitable range.
Thank you very much! This is really helpful and just what I needed to go in the right direction. I apologize for being a bit vague.
No problem. Happy to help.
For clarification, in case that post was too messy to comprehend, you should use raycasts for each from appropriate locations.