Most Efficient and Performance wise method for Cube-like Hitboxes?

Hello! The title explains it all. In my game I am planning to have 20 - 30 NPCs along with maybe a 12 or 15 player count. So far I’ve heard of two methods and that’s: .Touched with :GetTouchingParts() and Region3 (Rotated Region 3). I don’t want to use Raycasting or Magnitude mainly because those do not have a size and in my case I need a sized hitbox. I am unsure whether or not I should use Method 1 or Method 2 to achieve this, mainly because I don’t know which one is more performance friendly. If you have any other methods to achieve this please let me know.

Both of those two methods are flawed. Touched events (as well as GetTouchingParts()) are unreliable and often don’t detect things that are moving too fast (such as fast-moving weapons). Region3s are imprecise and can’t be relied on to precisely detect hits.

If you’re talking about performance, however, both are performant enough for you to discard any worries with just 20-30 NPCs and 12-15 players. If you really want to discuss performance, Touched events are the most performant because they are handled by the internal C++ physics engine, which runs much faster than Lua.

As for your aversion to Raycasting, I believe you have a misunderstanding about how they could be used. One very good method I happen to know involves using raycasts to trace the path of a moving weapon (FastCast module for ranged, RaycastHitbox for melee). If it can’t be represented by a single raycast, RaycastHitbox fires raycasts all along the length of a weapon all across the path of the weapon. It’s precise, accurate, reliable, and performant. If you want to use a cube-like Hitbox, fine, you can create an invisible part around the characters and NPCs but rather than letting unreliable Touched events determine when they are touched, Raycasts can determine that better.

1 Like

What scenarios are there when Region3s are imprecise? Could you give me some examples? Also just to clarify when I said Touched with GetTouchingParts I was referring to something like this Simple trick to make GetTouchingParts work with non-CanCollide parts Also if I created an invisible part I would have to make many raycasts to fill up the invisible part based on the size correct? Would this be better in terms of performance than Region3

If you use rotated Region3s aligned with the character and your hitbox is a cube, detecting a weapon within the Region3 can be precise but that brings the performance issues of constant searching for parts within that Region3. When it comes to performance, events are a lot more performant than loops. Although this still shouldn’t be too much to handle, it’s still exponentially more expensive and that you may want to consider.

As for raycasts, you are misunderstanding their use again. They are not used for filling up or representing a hitbox, they are used for seeing if a weapon touched a hitbox. If you read the paragraph I wrote and see the links I gave you, there are visual demonstrations on how they are used.

As for the Touched events, yes, that was precisely the use I was talking about. Regardless, Touched events are unreliable for detecting fast-moving objects moving through a hitbox, which would describe most weapons and combat systems.

1 Like

I see. I misunderstood you when you said I could use raycasting with an invisible part. The projects I make sometimes are fighting games. The combat is mainly based off Xenoverse 2 so I am actually speaking about this on the basis of hand-to-hand combat systems. Whenever I punch someone, I would kind of like to do that in a cube hitbox aswell just to make sure it actually hits my enemy most of the time when linking up combos. So in conclusion two hitboxes. One being the hitbox around the player, another being the hitbox created created on your attack. If a Rotated Region3 isn’t performance friendly, then could I fill up the second hitbox I mentioned for the attack with many raycasts without any performance issues?

Raycasts, in short, are not meant for filling up spaces. They’re meant for tracing paths. If you have a swinging fist, then it would create several raycasts tracing the entire path of the fist and if any of those raycasts touches a hitbox part, then it would deal damage. This can be achieved with the RaycastHitbox module I showed you above.

I certainly see and understand your point but this doesn’t achieve the goal of what a Square or Sized Hitbox is supposed to do however. That’s why I brought up filling a cube with many raycasts

By “cube-like hitbox” you mean a hitbox for getting hit, yes? I wouldn’t know the point of insisting that you detect the hand like a swinging cube when tracing the path is virtually the same. You can have a cube-like hitbox and detect when a swinging first touched it with a raycast.

If you insist on having the hand like a cube, then you can create four rectangular damage points on the hand from which the raycasts are fired and this would replicate the motion of a moving cube.

What I mean by a cube hitbox is a hitbox that has a size. It has an width, height, and length and if another Hitbox (Player’s Hitbox) enters any of that given area they will be damaged.

That’s what I thought: a hitbox for getting hit. This could be a cubic part. However, it would not use Touched events for detecting when it’s hit as those are unreliable; it would use raycasts on the fist that’s hitting it.

Please see the visual demonstration in the RaycastHitbox module post I gave you a link for above. If you have the hitbox as a rectangular part, then you can detect hitting with raycasts. The hitbox is a part. The damaging motion that hits it comprise of raycasts.

Did you know you can detect when a raycast touches a part? :wink:
That’s what I’m talking about. workspace:FindPartOnRay()
The RaycastHitbox module conveniently does this for you and provides its own OnHit event.

Yes I did know that. So in general the most performance friendly method is just to use a Part as a Player Hitbox and Raycasts for Attacks that enter the Player Hitbox?

No; just talking about performance, if I were to be honest, it’s still the Touched event because that’s hardly any work required at all.

However, raycasts are still extremely performant and not much more expensive than Touched events. It’s the #1 most precise and very reliable.

But yes, my point was to use a cubic part for the hitbox and the raycasts for attacks that enter it.