How can i prevent Light clipping?

hey guys,

Ive spent weeks trying to figure out a solution, but im at a loss here.

im trying to figure out how to prevent flashlight on guns clip through walls, as seen below

ive tried moving the light further back, which just causes weird shadows.
ive tried moving the light into the player, which also causes weird shadows.

im experimenting with using raycasting to see if there’s any obstruction between the flashlight and the player, but even that has caused some issues.

anybody have any ideas as to where i should begin?

I found the solution with a bit more experimenting with Raytracing.

heres the code incase anybody else comes up with the same issue as me

local ignoreList = {}

local Character = game.Players.LocalPlayer.Character
local Player = game.Players:GetPlayerFromCharacter(Character)

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist



for i,v in pairs (workspace:WaitForChild('Map'):GetChildren()) do
	if v.Name == 'Foliage' then
		table.insert(ignoreList,v)
	end
end

for i,v in pairs(Character:GetChildren()) do
	if v.Name ~= 'HumanoidRootPart' then
		table.insert(ignoreList,v)
	end
end

table.insert(ignoreList,script.Parent.Parent)

raycastParams.FilterDescendantsInstances = ignoreList

function Loop()
	
	local Origin = script.Parent.Position
	local Destination = Character.HumanoidRootPart.Position

	local Direction = Destination - Origin



	local raycastResult = workspace:Raycast(Origin,Direction, raycastParams)



	if raycastResult then
		local hit = raycastResult.Instance
		if hit.Parent.Name == Player.Name then --and script.Parent.OverRide.Value == false then
			script.Parent.Flashlight.Enabled = true
			script.Parent.Parent.Clipping.Value = false
		else
			script.Parent.Flashlight.Enabled = false
			script.Parent.Parent.Clipping.Value = true
		end
	end
end	

game:GetService("RunService"):BindToRenderStep('loop',1,Loop) 
4 Likes

Enable the “Shadows” property shared by all of the various “Light” instances.

that wasnt the issue.

the issue was flashlights on the gun, close to the muzzle that would clip through walls along with the gun, basically putting the flashlight on the other side of the wall physically