Trouble with melee raycast hit detection

So I was changing my hit detection system for my melee weapon to use raycast instead of .Touched(), and I’ve run into a problem.
I’m trying to have it so it raycasts between the position of the attachment (multiple attachment on blade of weapon for positions of rays) in the previous step, but I’m having trouble figuring out a good way to do that.

Code:

local oldPos = Vector3.new()
Run.Stepped:Connect(function()

	if tool.Blade:FindFirstChild("Swinging") then
		for i, v in pairs(tool.BodyAttach:GetChildren()) do
			if v.Name == "Ray" then
				local origin = v.WorldPosition
				local direction = oldPos - v.WorldPosition
				local params = RaycastParams.new()
				params.FilterDescendantsInstances = {plr.Character, workspace.WeaponsIgnore, tool}
				params.IgnoreWater = true
				local raycast = workspace:Raycast(origin, direction, params)
				-- Part to visualize raycast.
				local part = Instance.new("Part")
				part.Anchored = true
				part.Size = Vector3.new(0.05, 0.05, (oldPos - origin).Magnitude)
				part.CFrame = CFrame.lookAt(oldPos, origin) * CFrame.new(0, 0, -(oldPos - origin).Magnitude/2)
				part.Material = "Neon"
				part.CanCollide = false
				part.Transparency = 0.6
				part.Parent = workspace.WeaponsIgnore
				game:GetService("Debris"):AddItem(part, 0.1)
				
				oldPos = v.WorldPosition
                          end
                  end
         end
end)

(This is on the server)

So currently, this is what it currently looks like:

And this is what it should look like:
Screenshot 2022-04-08 184533

So what it does right now is looping through the attachments, and raycast between those attachments because oldPos is the position of the previous attachment in a way.(?) What I want it do do is, for each attachment, raycast between the previous position of the attachment (the variable, “oldPos”) and the current position of the attachment.

1 Like

Can I see the position of your attachments within the sword?

Have you tried Raycast Hitbox?

Edit: wrong module

instead of raycasting why not use

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartBoundsInBox

or

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartBoundsInRadius

this functions will return a list of all parts inside a box or ball

image

While I don’t know how to fix your problem, I will suggest you use a different method for hitboxes. Raycasting for hitboxes is something that should only be used when precision is necessary in my opinion. It is more server-heavy than other options and is harder to work with.

You could try magnitude + dot products to achieve a more optimized and simpler result.

1 Like

Sure
Screenshot 2022-04-08 203452
#1: (0, 0.453, 0)
#2: (0, 1.436, 0)
#3: (0, 2.173, 0)
#4: (0, 3.036, 0)
#5: (0, 3.959, 0)

Although I have considered it, I’ve been trying not to use any modules for learning purposes.

Alright, I found a fix. I used Vector3 Values in each attachment. I don’t think I explained what I wanted and what my problem was very well, but I appreciate the suggestions :slightly_smiling_face:

for the ones who will have the same problem in the future like me :

i asked why using vector3values would fux the problem … and i figured out why

its because the oldpos is shared for all the attachments on the sword you have to save the oldpos for each attachment Individually so each attachment have its own old and current position

note : i guess that you probably can do that in the script insted of having vector3values for each attachment on the sword