Issue with raycasts going through the wall

Hey there, I am basically complete with a raycast R6 gun system. I am having problems with the raycast going through walls however. If the weapon is inside of the wall, the raycast will have no problem traveling through that wall to the Mouse.Hit, and I want to fix that. I asked Deepseek AI and it did not solve the issue. The function is below.

function Gun_Class:PerformRaycast(origin, direction)
    -- First check if gun barrel is inside any wall/object
    local barrelCheckParams = RaycastParams.new()
    barrelCheckParams.FilterDescendantsInstances = {Player.Character}
    barrelCheckParams.FilterType = Enum.RaycastFilterType.Blacklist

    -- Check in 6 directions (up, down, left, right, forward, back)
    local directions = {
        Vector3.new(1, 0, 0), Vector3.new(-1, 0, 0),
        Vector3.new(0, 1, 0), Vector3.new(0, -1, 0),
        Vector3.new(0, 0, 1), Vector3.new(0, 0, -1)
    }

    local hitCount = 0
    for _, dir in ipairs(directions) do
        local result = workspace:Raycast(origin, dir * 0.5, barrelCheckParams)
        if result then hitCount = hitCount + 1 end
    end

    -- If barrel is inside a wall (at least 4/6 directions hit something)
    if hitCount >= 4 then
        return false -- Completely block shooting
    end

    -- Normal shooting raycast when not in wall
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Player.Character, self.Gun}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    raycastParams.IgnoreWater = false

    local raycastResult = Workspace:Raycast(origin, direction * Configuration_Module.Details[self.Gun.Name].Max_Distance, raycastParams)

    if raycastResult then
        local hitPart = raycastResult.Instance
        local hitPosition = raycastResult.Position
        local hitNormal = raycastResult.Normal

        -- Create beam effect
        local beam = Instance.new("Beam")
        beam.Attachment0 = self.Gun.Scripted.Barrel.Attachment

        local tempPart = Instance.new("Part")
        tempPart.Anchored = true
        tempPart.CanCollide = false
        tempPart.Transparency = 1
        tempPart.Size = Vector3.new(0.1, 0.1, 0.1)
        tempPart.Position = hitPosition
        tempPart.Parent = workspace

        local hitAttachment = Instance.new("Attachment")
        hitAttachment.Parent = tempPart
        beam.Attachment1 = hitAttachment

        beam.Width0 = 0.1
        beam.Width1 = 0.1
        beam.Brightness = 10
        beam.Color = ColorSequence.new(Color3.fromRGB(106, 60, 255))
        beam.FaceCamera = true
        beam.Parent = workspace

        Debris:AddItem(beam, 0.25)
        Debris:AddItem(tempPart, 0.25)

        self:ProcessHit(hitPart, hitPosition, hitNormal)
        return true
    end

    return false
end

Help is appreciated, thanks!

Instead of Shooting 6 Raycasts each direction, you can try using either GetPartBoundsInBox, GetPartBoundsInRadius or GetPartsInPart. Raycasts arent that good since all the raycasts might be in the wall at once

Both raycast params I’m seeing have their filter type set to blacklist, which looks like a depreciated enum to me, since it has a strikethrough in Studio’s IDE and isn’t mentioned in the docs. Use exclude instead for a more predictable behavior.

With that said, it won’t solve your problem of the raycast going through walls. Your raycast params tell your raycasts to completely ignore everything that isn’t a player’s character, allowing them to go through everything until it hits a character. Use if statements to figure out if the raycast instance is Player.Character instead.

If a Raycast’s Origin is Inside and Object, it WILL ALWAYS ignore that object no matter what… since i believe Raycasts ONLY acknowledges the outer/outside hitboxes of an Object… So, only way to fix this is to either have 3D hitboxes check first around the Ray Origin, or moving the ray origin way back until it cannot go inside an object

I have experienced the same problem before, try making an attachment inside the barrel, to fire a ray front and back to prevent shooting through walls.

local startAtt = --any attachment lodged inside gun barrel

local function raycastfiltering(plr)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {plr.Character}
	params.FilterType = Enum.RaycastFilterType.Exclude
	return params
end

local function getStart(startPos,plr)
	local gunAttPos = startAtt.Position
	local rayCastParams = raycastfiltering(plr)
	local ray = workspace:Raycast(gunAttPos,startPos-gunAttPos, raycastParams)
	if not ray then
		ray = workspace:Raycast(startPos, gunAttPos-startPos, raycastParams)
	end
	if ray then
		local hit = ray.Instance
		startPos = ray.Position
		return true	
	end
end

if getStart(startAtt, player) then return end

Thanks for the advice. I used some of this alongside the other information to greatly improve and fix my raycast function.

I tried this and it only worked with the other data I collected, but it still helped out. Thanks!

Thank you, I made it also check around the bounding box.

Thanks, I implemented one of these functions and it worked out fine with my new raycast setup.

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