Raycast Hit Detection Innaccuracy?

Well I am trying to make a Hit detection system for a sword using attachments and Raycasting,

In-short, I basically continuously Raycast, during the swing, from the attachments current position to its last position. (screen shot included below)

My issue is that the EVEN THOUGH I have my Raycasts visualized through parts, I’ve noticed that even when my parts don’t go anywhere near the Rigs I’m testing on, it still registers as a hit…

Here’s a screen shot of the in-game visualized Raycasts:

Now, I had set up print statements to see what I was hitting, here they are:
image

As you can see, even though the Raycasts go no where near rig1 or rig3, they seem to be getting hit somehow, I’m just wondering why this is happening and how to fix it, thank you!

Here’s loop for the actual Raycasting during the swing:

slicing = true
			while slicing do
				if waitSwitch == false then
					waitSwitch = true
				else
					task.wait()
					waitSwitch = false
				end
				
				for _, att in ipairs(bladeAttachments) do
					local attNum = string.sub(att.Name,4,4)
					local attNum = tonumber(attNum)
					
					local origin = bladeAttachments[attNum].WorldPosition
					local direction = lastOrigins[attNum]
					local distance = (origin-direction).Magnitude
					
					local params = RaycastParams.new()
					params.FilterType = Enum.RaycastFilterType.Exclude
					params.FilterDescendantsInstances = {workspace.Map, workspace.Fx, char}
					
					local result = workspace:Raycast(origin, direction, params)
					
					
					if result then
						table.insert(partsHit, result)
					end
					
					lastOrigins[attNum] = origin
					
					if debugMode then
						local debugPart = Instance.new("Part")
						debugPart.Color = Color3.fromRGB(255, 0, 4)
						debugPart.Material = Enum.Material.ForceField
						debugPart.Size = Vector3.new(0.2,0.2,4)
						debugPart.CFrame = CFrame.lookAt(origin, direction)*CFrame.new(0,0, -distance/2)
						debugPart.Anchored = true
						debugPart.CanCollide = false
						debugPart.Parent = workspace.Fx

						game.Debris:AddItem(debugPart, 2)
					end
					
				end
				
				--print("slicing!")
				
			end
		end)

Help is appreciated.

1 Like

The direction you use is the target position of the ray.

The direction vector should be ‘targetPos - startPos’. I.e. destination minus beginning.

2 Likes

Wow, that just made it insanely accurate…

local origin = bladeAttachments[attNum].WorldPosition
					local direction = lastOrigins[attNum]-origin

Those are the changes I made, is that along the lines of what you were talking about?

Little weird thing happens now though, my visualization through parts is superrr wonky:


(ignore color change)

For the debugpart’s orientation you should make it look at the target position, and not the direction. But I assume it works now?

Yes works very well, thanks for all of the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.