Why is it that when you ignore collision groups, raycasts are displaced?

Hey folks, I’m not exactly how you can describe this situation other than “displacement”, so I haven’t found other topics on this.

I am working on a gun system and currently I am using raycasts to get the end position of a bullet and also have certain groups ignored.
Problem is, when I do put stuff in collision groups, it ends up displacing my bullet. Let me show you an example:

(Gray box is a bullet end position)

image
This is when I do not go through a collision group. (Red is the raycast’ original point).

image
This is when I DO go through a collision group. (Black is the expected position).

I’ll provide code if need be, but I feel like this isn’t an issue with my own code and rather just how Roblox works.

Code Just in Case
function Raycast(Player, StartPosition, EndPosition, Settings)
	local RaycastParameters = RaycastParams.new() --Creates new parameters.
	RaycastParameters.FilterDescendantsInstances = {Player.Character, workspace.RaycastIgnoreList} --Prevents the player from being in the raycast.
	RaycastParameters.CollisionGroup = "IgnoreList"
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist --Blacklists.

	local Raycast = workspace:Raycast(StartPosition, (EndPosition - StartPosition).Unit * Settings.MaxDistance, RaycastParameters)
	return Raycast
end

	local RaycastResult = Raycast(Player, Origin, EndingPoint, Settings)
	if RaycastResult then
		local Part = Instance.new("Part")
		Part.Parent = workspace
		Part.Position = RaycastResult.Position
		print(Part.Position)
		Part.Anchored = true
		Part.Size = Vector3.new(0.2,0.2,0.2)
		DebrisService:AddItem(Part, 4)
	end

Here’s what I used to configure collision groups:

local function IfStatements(part)
	if part:IsA("BasePart") and part.Name ~= "Terrain" then
		if part.Transparency > 0.7 then
			PhysicsService:SetPartCollisionGroup(part, "IgnoreList")
		elseif part.Material == Enum.Material.Glass then
			PhysicsService:SetPartCollisionGroup(part, "IgnoreList")
		end
	end
end

for i, part in pairs(workspace:GetDescendants()) do
	IfStatements(part)
end
for i, part in pairs(ReplicatedStorage:GetDescendants()) do
	IfStatements(part)
end


for i, part in pairs(ServerStorage:GetDescendants()) do
	IfStatements(part)
end
1 Like

After some checking, it seems like it doesn’t matter if it’s cancollide true or false, which is weird.

I also figured out that if I walk too close to the wall and try shooting, the bullet doesn’t even show up anywhere nearby, maybe it just got shot out to the stratosphere. No idea of a fix.

Bumped due to the urgency of this; this is one of the last things I need to do for this script.

Bumped once more. I tried using Mouse.Hit.Position instead of an old method and it still ended up having the same issue.

Bumping, I looked into the possibility of Mouse.Target and FilterTarget, but I feel like it would be a pain to get it working with collision groups.

I’m starting to think that maybe the rays aren’t going directly through and rather appearing in a model like this:

Not sure myself, but it’s a possibility.
Seriously though, lost at a fix.

Created a reproduction file as well as a video.
GunRepro.rbxl (64.6 KB)

What’s going on is that yes you have the part filtered by the ray, but not the mouse. So what happens is raycast is ignoring the part but since the mouse isnt ignoring the part the end position is going to be on the part and so it changes the direction of your bullet to the gun to that point on the wall and not the gun to the point through the wall. To fix this, use Mouse.TargetFilter.

1 Like

How would you suggest I go about ignoring entire collision groups with TargetFilter?

I do not think the mouse has any way to ignore multiple instances that are not descendants of the ignored instance, so you may need to write your own mouse class or something similar using ContextActionService and/or UserInputService. These services allow you to determine where your mouse is on the screen, then you can use ScreenPointToRay and then raycast with your parameters to determine the world position.

You can put everything you want the mouse to ignore as a child of the same model.

1 Like