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!