How to make a perfect hitbox?

I’m going to make a combat system, and I want to know what hitbox methods you guys would use because I have no clue to go about this.

Raycasting would work, but it would be sloppy as I’d probably have to fire several rays in directions AND time when to check if they hit a part (while knowing a ray may not end up hitting the part).

Magnitude works, but it also can be messy as i’d have to loop through every player per click + if a player clicks and punches it could end up punching someone from behind.

How would you guys go about making a hitbox that almost 100% hits?

10 Likes

I just use .Touched event, it’s simple and reliable but some what bad for High latency.

However if you want near perfect Server - Client communication it’s going to be a lot more complex.

Of course you could try combining .Touched, RayCasting and Manitude

2 Likes

Use a cone/angle system.

Have a reach and a spread on each of your attacks (vertical and horizontal, if you like) and anything within that area (idk how you’d figure that out but whatevs) gets hit.

2 Likes

Ray casting is only expensive for long distance rays. If you make a few short distance rays every frame, you will be fine. Here’s what I recommend:

  • Figure out where the projectile will be next frame (moving projectile via cframe)
  • Cast a ray to that point. If the point if intersection is in the hitbox, or the part found is the hitbox, then count the hit.
  • If the projectile did not hit, move it forward. Keep repeating these steps until the projectile spawns or hits.

If you have questions about this method, feel free to ask!

If it is just a melee system follow bullet 2.

3 Likes

You could weld large enough invisible parts to the players hands or feet (that would in essence be a hitbox) and then use the touched event along with a cooldown variable.

local hitbox = --the invisible part here
local isCoolingDown = false
local COOL_DOWN_TIME = 3-- in seconds
local DAMAGE_PER_PUNCH = 20
hitbox.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChild("Humanoid") and not isCoolingDown then
       isCoolingDown = true
       local character = hit.Parent
       character.Humanoid:TakeDamage(DAMAGE_PER_PUNCH)
       wait(COOL_DOWN_TIME)
       isCoolingDown = false
  end
end)

and also to prevent damage when the player is not punching or kicking, you would only weld the hitbox when the player is using the punching move and remove it after.

2 Likes

Ranged hitboxes I think are pretty straightforward to do, so I don’t think I need to go over that (besides, I think these other lovely people can give more insight on ranged combat if you need that).

For melee hit boxes, I use raycasting. A few years back, I’ve been intrigued by the design of MORDHAU and Chivalry since I wanted accurate hitboxing while making it simple to do across a variety of different weapon shapes and sizes. Here’s a timestamp of how MORDHAU did their hitbox for their swords which may prove to be helpful.

Warning: Does include realistic depiction of blood so please be aware before opening

A few examples that I’ve done that uses this system. It basically fires a ray from an attachment placed on the weapon every frame. Do note that the red lines are visualizations purposes only, they do not last that long.

I also made it so you can adjust the placements of where the raycasting starts via attachments so I can fit it on different weapon shapes easily without hardcoding it. Here is the same system but the attachments are spread out a little to compensate for larger hitboxes.

In the past I’ve used GetTouchingParts and Region3 for these stuff with angle checks, but that proved far too performance heavy for the type of game I was making. I was surprised, the performance was actually improved after switching over to this system since while, yes there are a lot of rays being made in a short span of time, they were also really small so the performance drop was negligible.

Edit: Another big plus on using raycasting for melee weapons is that, you can make really fast and fluid melee combat while always retaining accurate hitboxes. Using touched or similar methods I’ve tested, sometimes when the move goes too fast, the physic engine doesn’t register it.

91 Likes

I’m gonna go ahead and be the guy who ask, If you don’t mind of course

Can you show us how you set up the Attachments and provide us with Sample code?


From what I comprehended so far; you use RayCast to detect Hits but I’m not sure of how the rest of the system works.

2 Likes
12 Likes

This is really cool. I’m making a game and was thinking about hitboxes during early stage before I got to them on my to-do list. I was thinking of just using touched event by player touching attacking players parts. Or for bigger bosses and such actually creating a transparent hitbox but yes this is soooooooo much more effective and free. And also makes a defense system way easier to implement.

2 Likes

Guys, about the raycasting method, in which direction do you cast the ray? I get that you plant attachments on the surface and then fire a ray from there, but then what’s the direction, is it (0,0,0) ?
by the way I’m asking for melee combat. :smiley:

2 Likes

For me, I did the direction by the rightVector of the blade, if you want to make the ray shoot from the side of which it is facing, then use lookVector.

3 Likes

How would I add knockback with this?

3 Likes

body velocity. 30 tttttttttttttttttttttttttttttt

2 Likes

will it work for ranged weapons?

2 Likes

I use FastCast for everything ranged.

Also for melee I prefer to make everything super competitive, so I make it server-sided.
If anyone wants to know more, I’ll probably make a tutorial on my code design for melee systems.

1 Like

late reply but i do lol, i used the hitbox raycast module and it works but it only registers someone getting hit sometimes and other times it just does not work.

1 Like