Raycast breaks when ignoring tagged groups

I don’t know how to make a better title, but I’m making a game where the OTS (over the shoulder) camera goes through objects and you’re supposed to see through them.

The thing is, it seems that whenever I unwrap only the tagged objects, it breaks and points off to another direction:

Here’s the code for it, I know what inviscam exists but when I set the occlusion to that using scripting, it doesn’t work:

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {unpack(workspace.BooletZone:GetChildren() --[[bullets]]), unpack(Char:GetChildren()--[[Character]]), Camera, unpack(CS:GetTagged("(hidden)")--[[tagged hidden objects]])}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	local MouseHitPos = Mouse.Hit.Position
	local RayDistance = (Camera.CFrame.Position - MouseHitPos).Magnitude + 1   

	local RaycastResult2 = workspace:Raycast(Camera.CFrame.Position, CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * RayDistance, raycastParams)
	--print(unpack(CS:GetTagged("(hidden)")))
	local Hit
	
	if RaycastResult2 == nil then
		Hit = CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * RayDistance --if the raycast result is null, it gives a simular "solution"
	else
		Hit = RaycastResult2.Position
	end
1 Like

this is quite the old post, and I may be wrong, but I don’t think you need to unpack the children of a model. what I usually do is instead simply just put the model inside the table. I read the documentation of FilterDescendantsInstances and apparently it literally gets the descendants and puts it into the table.

also, there’s marker on the ground and I noticed that when the raycast “breaks”, it points towards it, or at least near it. do you have any code related to that?

Oh yeah, that marker is unrelated, sorry. I meant the torso and arm glitching.

And I just found add to filter, so i think I’ve gotten the wrong mothod to this raycast. Ill try it later and ill see if it fixed my problem.

So, I just did what you said, I think, but it still gives the same issue:

And here’s the updated code

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.BooletZone, Char, Camera, unpack(CS:GetTagged("(hidden)"))}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	local MouseHitPos = Mouse.Hit.Position
	local RayDistance = (Camera.CFrame.Position - MouseHitPos).Magnitude + 1   

	local RaycastResult2 = workspace:Raycast(Camera.CFrame.Position, CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * RayDistance, raycastParams)
	--print(unpack(CS:GetTagged("(hidden)")))
	local Hit
	
	if RaycastResult2 == nil then
		Hit = CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * RayDistance --if the raycast result is null, it gives a simular "solution"
	else
		Hit = RaycastResult2.Position
	end:

also, doing that also tweaks out the ray when intersecting a bullet:

I have no idea what to do, but it also sets the raycast to nil when i use the print function.

Oh wait, never mind, the raydistance is really short when the mouse’s distance to the camera is very short.:

local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.BooletZone, Char, Camera, unpack(CS:GetTagged("(hidden)"))}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	local MouseHitPos = Mouse.Hit.Position
	local RayDistance = (Camera.CFrame.Position - MouseHitPos).Magnitude + 1   

	local RaycastResult2 = workspace:Raycast(Camera.CFrame.Position, CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * RayDistance, raycastParams)
	--print(unpack(CS:GetTagged("(hidden)")))
	local Hit
	
	if RaycastResult2 == nil then
		Hit = CFrame.new(Camera.CFrame.Position, MouseHitPos).LookVector * 1000 --if the raycast result is null, it gives a simular "solution"
	else
		Hit = RaycastResult2.Position
	end:

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