Best way to go about punching system collision detection?

This seemingly simple question is somehow difficult to me. What’s the most accurate and best way to make a punching system?

I know that I can do raycast, magnitude check, and touched event. However, touched events are unreliable, almost never registering any hits, and the best way I went about doing it was making an invisible hitbox part but since it was unreliable, constantly creating a new hitbox seemed to fix its reliability but I doubt it’s a good idea to spam it. Magnitude checks are based on distance and it seems more expensive in resource usage. Raycast sounds the best but I couldn’t get it to work at all (Fix raycast punching system).

If anyone here has ever created an efficient punching damaging system, what method do you use?

2 Likes

I use raycasting, as raycasting being quite new technology in roblox, i can tell you its one of the best tools for guns and other fighting mechanics, i highly recommend you use raycasting! If you are having trouble try using projectiles!

4 Likes

I’m not asking for any code specifically here, but could you explain your raycasting in detail? Are you firing multiple ray, where is it coming from specifically as a position?

What do you mean by using projectiles? Throw a hitbox forward from the fist?

1 Like

Yes, I am firing multiple rays.

1 Like

Ping/Network lag is always a big factor.
I’ve been thinking about this a lot and there’s no perfect solution.

I recommend having the client side do the collision detection.
Then have the server side use magnitude for sanity checks.

Yes, this post is for that client side collision detection. We can ignore lag for now because my main goal is to get an efficient collision detection system for punching.

This hitbox module is pretty useful for that

1 Like

When the player presses a hotkey assigned to punching, you should play an animation and then fire a ray x studs forward in the direction the HumanoidRootPart is facing. Raycasting is going to be a lot more reliable than BasePart.Touched.

2 Likes

I want to try creating something of my own. Borrowing a public module is useful but this is for learning sake. :slight_smile:

@liuthemoo Is firing directly from in front of the humanoidrootpart a good idea? The target may be in arm range but not infront of the body. I do agree that raycasting is more efficient than using Touched.

Also to people replying, ignore animations & server-side sanity checks. This is just for the collision detection part of punching.

3 Likes

The way I did it was have a damage script that was disabled with the touch event in it on a debounce.
When a key is pressed to punch, the script is cloned and put into the arm that is punching turning it into a hitbox and then the script is destroyed when the animation completes. It takes some trial and error to match the damage window with the animation and swipe/hit sounds, but I have found this to be consistent.

1 Like

Touched isn’t reliable though, are you sure you have not had a moment or two where it didn’t register a hit?

At first I did, but after testing, I realized the hit was trying to register too soon and the damage window was smaller than my animation allowed. Meaning the damage would be on when I reared back to punch and when the fist would make contact, the damage would be off. I could tell be cause the damage was being done slightly before the hit was making contact. Then I moved it to too late, and it didn’t look right doing damage after the sound hit and animation was over and my fist was coming back to idle. I also have outside hit boxes in the settings menu if that makes a difference. idk tbh.

1 Like

I agree, but I think mostly people tend to punch more or less in front of themselves as opposed to off to the side. You could maybe fire multiple raycasts with a slight offset?

1 Like

You’re right but I can’t actually even make my raycast work lol. Also, I’m not even sure where to fire rays from since your arm can sometimes be inside of your enemy.

Wdym by you can’t make your raycast work? Do you know the specific part that isn’t working? Are there any errors in the output? If you fire 3 rays from the right, middle, and left of the HumanoidRootPart, which won’t be inside of an enemy, I think that it should work.

1 Like

I already have a hyperlink in the post if you’re interested. I mean actually my raycasting is insufficient and looks buggy.

I highly recommend using this module if you wanna use raycasting, it works very well, and is easy to use!

Could try using explosions for hit detection as well.

Someone already posted about that and I already use it, just that I want to try making my own system from scratch.

1 Like

I was working on a project today and my attacks were really inconstant on a new enemy because it was smaller than other enemies. To fix this I welded an invisible part to the npc and use that as a hitbox. Now my attacks are not missing even when the enemy is above/below your or a smaller size.