Hello, i was wondering what the best way to write a script for melee fighting is. I assumed it would be to use ray casting, but i don’t know if that would be the best way to go about it?
There are lots of good ways to do it. The one I usually go with is to do a .Touched event on the client when your sword hits an enemy. Then send a message to the server and do a distance check to make sure that they were in range of the hit so there’s no exploiting. You could go a lot more in depth with hit tables and debounces but that method should get you started fairly quickly!
Note: The Touched event is often considered to be inaccurate, however when you use it on the client like I’m suggesting its near perfect!
would be to use ray casting
It depends on the type of attack, but usually rays/region3s are best.
Assuming you’re going with the raycasting approach, without going into too deep about how raycasting works, you’ll want to only cast rays whenever a player attacks and make sure it’s only checking for player hits, and not the entire workspace. This other post on the devforum explains it better than I can.
If you change your mind and decide to use Region3s, I recommend using EgoMoose’s RotatedRegion3 module; the module and it’s tutorial can be found here
It should also be noted that, unless you’re doing something slow similar to how Black Magic does it, you should try to keep the hit detection on the client.
I don’t suggest using Touched if you’re looking for a player collision.