Filling gaps inbetween spatial melee attack hitboxes

Edit: This post was created before the introduction of Shapecasting, which is a much more effective solution for hitboxes.

I’ve been trying to make a hitbox system using spatial queries and OverlapParams. When creating a series of hitboxes for melee attacks, faster animations tend to leave gaps between the hitboxes even when they are created every frame:

RobloxStudioBeta_pq9jT1nR4Z

This would be even worse if a player had a lower framerate, so to solve this I was trying to create one or more “inbetween” hitboxes to fill in most of the empty space. I looked around the forum for other posts related to the problem and ended up with the following code and results:

-- Simplified
function Inbetween(part1, part2)
	local distance = (part1.Position - part2.Position).magnitude
	local part3 = part1:Clone()
	part3.Size = Vector3.new(part3.Size.X, part3.Size.Y, distance)
	part3.CFrame = CFrame.new(part1.CFrame:Lerp(part2.CFrame, 0.5).Position, part2.Position)
	part3.Parent = workspace
end

The red parts are the main hitboxes, while the blue parts are the inbetweens created by the script. You can see that the positions of the inbetween parts are close to what I need, but the rotation and size are still off.

The issue here is that the solution seems to rely on having a specified length, width, and/or thickness and isn’t accurate on every axis - two parts with sizes of (1, 5, 1) and (1, 1, 5) rotated to appear the same will give different results. Since the hitboxes could have any size and rotation, I probably need something completely different.

The way I handle mine is raycasting. I have a script that lets me either create the raycasting points individually or just fill a hitbox with. It just casts between where the tracking points were last frame and where they are this frame.

The way I set my script up, it would only track the points when a player started an attack. so it’s a lot like what you are trying with the parts specifically the in between parts. It also exposed a function to detect hits and would only give each player it hit once with the hitlist automatically clearing when it’s activated at the start of an attack by default.

Picture examples

image


image

The red part is purely a guide for placing the points. I don’t do any detection with it. It’s only visible here to show how I was generating points. For this weapon I actually just had it set to only track (1,1,13) points or something like that. (1 point in the parts x direction, 1 point along the parts y, and 13 along the parts z).

Also consider using this module that uses the same concept and would likely be a lot easier and faster to implement than setting one up yourself. Raycast Hitbox 4.01: For all your melee needs!

3 Likes

You’re right that raycasts are probably going to be faster and more accurate than whatever I’m trying to do here. I was already aware of the idea of using raycasts for melee hitboxes (as well as the Raycast Hitbox module) when I went into this, but I really wanted to make spatial queries work for some reason.

I’m kind of at a dead end with this idea though, so if there’s really no better way to fill the gaps between the parts using spatial queries then I’m probably just going to switch to raycasts.