Raycast detection barely working

A code I have written for raycasting barely detects enemy when it should, and yes I have visualized it yet it doesn’t seem to detect it.
Here’s the code

---Services
local RepStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

---Extras
local originPart = RepStorage:WaitForChild("Origin")
local endPart = RepStorage:WaitForChild("End")
local enemys = workspace:WaitForChild("Enemy")
local debuggingFolder = workspace
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.CollisionGroup = "Raycast"
raycastParams.IgnoreWater = true

---Functions
local function visualizeRaycast(origin,rootCframe,result)
	local originClone = originPart:Clone()
	originClone.Position = origin
	local endClone = endPart:Clone()
	if result then
		endClone.Position = result.Position
	else
		endClone.Position = origin + rootCframe.LookVector * 70
	end
	originClone.Parent = debuggingFolder
	endClone.Parent = debuggingFolder
	Debris:AddItem(originClone,7)
	Debris:AddItem(endClone,7)
end

function castAction(character)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	local rootCframe = rootPart.CFrame
	local affectedEnemy = {}
	local filter = {}
	local key = 0
	for i,v in ipairs(enemys:GetChildren()) do
		if v:GetAttribute("Enemy") then
			key += 1
			filter[key] = v
		end
	end
	raycastParams.FilterDescendantsInstances = filter
	for count = -10, 10, .5 do
		repeat
			local origin = rootCframe.Position + rootCframe.RightVector * count
			local raycastResult = workspace:Raycast(origin, origin + rootCframe.LookVector * 70,raycastParams)
			visualizeRaycast(origin,rootCframe,raycastResult)
			if raycastResult then
				print(raycastResult)
				local mob = raycastResult.Instance.Parent
				if mob:GetAttribute("Enemy") and not affectedEnemy[mob] then
					affectedEnemy[mob] = true
					for i,v in ipairs(filter) do
						if mob == v then
							table.remove(filter,i)
						end
					end
				end
				raycastParams.FilterDescendantsInstances = filter
			end
		until raycastResult == nil
	end
	print(raycastParams.FilterDescendantsInstances)
	print(affectedEnemy)
end

Now Here’s a video of it barely detecting

More info:
The green and red parts you see are the visualized raycast, The origin is the green and the endpoint is red.

I’m using collision group and the raycastparams filter descendants to filter out and ignore the unnecessary collision,I have a collision group named Raycast which will be the only that’s gonna be detected if it hits a part that is on it.

And as you can see I have a hitbox that is almost as big as the player and is on the collision group Raycast

I don’t really have no idea on why it barely detects it. (I’ve been having this issue for a day now.)

2 Likes

Simply removing the “origin +” out of the “origin + rootCframe.LookVector * 70” should do the trick

The second argument of raycast is a direction alone, not a direction and position

That should solve one problem with inaccuracy, other problems could come up though, I didn’t thoroughly read your code

1 Like

Oh god, Thank you so much for this solution bless you. And i’m actually really dumb T_T