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?
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!
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.
I want to try creating something of my own. Borrowing a public module is useful but this is for learning sake.
@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.
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.
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.
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.
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.