Hey, so I’m having an issue with some raycasting code.
Basically, if the origin (start position) of the ray is colliding with another part that I want the ray to hit, it won’t hit it, despite it being directly in the ray’s path.
This is a problem I’ve always been aware of with raycasting, and it just came up again as I was playtesting. I wondered if there was a bypass I could do with collision groups, although that might make it so the rays can’t hit those parts entirely.
I hope this post makes sense, if not I can try to clarify further.
function findPartOnRayWithOriginInsidePart(part, originPos, direction, raycastParams)
local function originIsInsidePart()
local positionPart = Instance.new("Part")
positionPart.Size = Vector3.new()
positionPart.CFrame = CFrame.new(originPos)
positionPart.CanCollide = true
positionPart.Transparency = 1
positionPart.Anchored = true
positionPart.Parent = workspace
local touchingParts = getNonCanCollideTouchingParts(positionPart) -- just in case the part is non cancollide
positionPart:Destroy()
for _, touchingPart in next, touchingParts do
if touchingPart == part then
return true
end
end
return false
end
if originIsInsidePart() then
return {
Instance = part;
Position = originPos;
Normal = direction;
}
else
return workspace:Raycast(originPos, direction, raycastParams)
end
end
FilterDescendantsInstances would only ignore the specific parts when the ray gets cast in a direction, not detect them individually, and I don’t believe it wouldn’t matter if you were to set the FilterType to a Whitelist or a Blacklist since the Ray would detect the first part that it hit then stop regardless
You could detect when the Raycast hit’s a desired object, and then continue the Raycast by sending another Raycast from the point it hit in the same direction it’s heading towards.