Any way to bypass this raycasting issue?

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.

Thank you!

I don’t believe there is a reference that allows a ray to pierce through parts, but you could use a loop perhaps?

I’ve always wondered this issue myself as well, and I suppose a search showed this

1 Like

Use RaycastParams to ignore/detect what you want, and are you using CFrame.LookVector?

1 Like

I believe he’s wanting the ray to pierce through all the parts? And not just ignore specific parts?

1 Like

It depends on how you set it, as FilterDescendantsInstances is a table, you can put everything you don’t want inside and put FilterType in BlackList

1 Like

If he wants the ray to not hit anything then you can set a whitelist to nothing.

2 Likes

This should do it :sunglasses:

(Not tested)

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
3 Likes

You could use fastcast comes with a built in function

1 Like

Still

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

1 Like

Nothing that a loop doesn’t solve, right?

while wait() do	
	local rayresult = workspace:Raycast(origin,direction,Params)
	if not rayresult then		continue/return		end
	print(rayresult.Instance)
end

And the problem of @C_Sharper is the distance of the ray, surely he is using CFrame.LookVector as direction.

2 Likes

Maybe you could use game:GetServer(“Runservice”).Heartbeat:Wait() instead of Wait().

2 Likes

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.

1 Like