Why does my raycast still activate through walls?

hello, i posted a topic on this earlier but nobody bothered to even help so here i am posting it again haha

anyway, my objective is to create a raycast where when a player looks at something (in this case, slender man), the object triggers a GUI that will appear on the player’s screen.

I scripted the raycast params and everything so i though it’d work. And it did, but it still triggered through walls for some reason

here is my code (the whole thing lol):

local camera = workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()

local p = game.Players.LocalPlayer

local c = p.Character or p.CharacterAdded:Wait()


local VisCheck = function(Obj)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	raycastParams.IgnoreWater = true
	local raycastResult = workspace:Raycast(camera.CFrame.Position, CFrame.new(camera.CFrame.Position,Obj.Position).LookVector*5000, raycastParams)
	if raycastResult ~= nil then
		if raycastResult.Instance:IsDescendantOf(Obj.Parent) then
			if Obj.Parent.Humanoid.Health > 0 then
				return true
			end
		else
			return false
		end
	end
	return false
end

local StartUITrack = function(Obj)
	local worldPos = Obj.Position
	local pos, isOnScreen = camera:WorldToScreenPoint(worldPos)
	local fixedposx = pos.x
	local fixedposy = pos.y
	local IsInLib,num = false, nil

	if isOnScreen then
		if VisCheck(Obj)== true then
			return true
		end
	else 

	end

	return false
end







while wait() do
	
	local mag;
	
	local range = game.Lighting.FogEnd - 2;

	local cansee = true
	
	local DummyHead = workspace.Slenderman["Meshes/torso (3)"]
	
	coroutine.wrap(function()
		while wait() do
			mag = (c.Torso.Position - DummyHead.Position).magnitude;
		end
	end)()
	
	local WG = workspace.Slenderman
	
	coroutine.wrap(function()
		while wait() do
			local Position, CanSee = camera:WorldToScreenPoint(workspace.Slenderman["Meshes/torso (3)"].Position)
			if CanSee and mag <= range and cansee then 

				local StaticMain = script.Parent.StaticGUI
				local Static1 = StaticMain.Static1
				local StaticSound = script.StaticSound

				StaticSound.Playing = true
				StaticSound.Looped = true

				StaticSound.Volume = 1 /mag * 2

				StaticMain.Enabled = true
				Static1.Visible = true
				Static1.ImageTransparency = 0.50 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.75 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.25 * mag / 8.55
				wait(0.1)
				Static1.ImageTransparency = 0.9 * mag  / 8.55
				wait(0.1)

			else

				local StaticMain = script.Parent.StaticGUI
				local Static1 = StaticMain.Static1
				local StaticSound = script.StaticSound

				StaticMain.Enabled = false
				StaticSound.Playing = false

			end;
		end;
	end)()

	local IsSeen = StartUITrack(WG["Meshes/thehead"])
	if IsSeen == true then

	end

end

any help is greatly appreciated, thank you in advance

1 Like
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

I’m no expert at raycasts but shouldn’t you add the walls to the blacklist? I would put all the walls in a folder or something (as long as it won’t break any existing code) and then add a reference of the folder to the blacklist.

1 Like

i’ll try that and i’ll update you if it works

1 Like

it didn’t work.
i don’t really know much about raycasting either to be fair

This could be one potential issue, where the origin of your Ray is from the camera’s Position. The reason why I think it is a potential issue is because if your head is staring in front while slender man is behind you, your camera could just face it and it will trigger the GUI. Unless you’re intended doing this, you should do some work.

the game is in first person anyway so it isn’t really much of an issue to me

I think this is the wrong approach.

Ray is an infinitely small on its width and height. So it wouldn’t really work if the object was not directly in front of your screen. The character Infront anyways.

Instead you can use the raycast to check if there is anything between the object and the player.

It will look like this

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType -- Blacklist
RaycastParameters.FilterDescemdant -- Character and Object
local RaycastResult = workspace:Raycast(CharHead.Position,Object.Pos - CharHead.Pos,RaycastParameters)
If RaycastResult then
    -- Something is between the player and object
end
1 Like

how would i actually go about inserting this?

Well. Not much really.

Though I recommend using Runservice instead of a while wait()

You simply run that code every frame.

You could use collision groups if you would like as well as they are much more easier to deal with.

You must also combine with some sort of FOV to only activate when the object is Infront of the player. Raycast will make sure that nothing will be between the player and the object.


Alternatively if your game is first person you can use a method to check whether or not the object is on the players screen or not which is only literally one function.