Raycast swerves, not reaching enemies past an ally at times

Hey there, so, I’ve been making a client gun script, and while polishing some aspects, I’ve ran into an issue - In the function “Shoot”, the local variable “res” and “intersection” gets the mouse.hit.position and uses a blacklist above it for allies to prevent them from blocking bullets, but mouse.hit ignores collision groups all together and is just set as “Default”, causing bullets to swerve completely off when I’m hovered at allies trying to shoot enemies from behind (Doing so from the head makes the problem much more prevalent and apparent).

I’ve left relevant chunks and context to get my problem across more easily. Help and guidance is appreciated, my problem I assume is the mouse.hit at both “res” and “intersection”. I’m just not sure how I could go about altering the code to fix this problem :sweat_smile:

local newHighlight = Instance.new("Highlight")
newHighlight.FillColor = Color3.fromRGB(199, 255, 201)
newHighlight.Parent = nil

game.Players.LocalPlayer:GetMouse().TargetFilter = workspace.IgnoreandClearonEndorStart --Ignore dead and effects

local Guncast = RaycastParams.new() --raycast for the bullets
Guncast.FilterType = Enum.RaycastFilterType.Exclude
Guncast.CollisionGroup = "Bullet" --detect parts under the default and player collision group, every limb will be on here except humanoidrootpart
Guncast.IgnoreWater = true
Guncast:AddToFilter(game.Players.LocalPlayer.Character,workspace.IgnoreandClearonEndorStart) --ignore user and effects 

local function DrawRecticle() --would constantly be called on every prerender connect when equipped

	local viewportResult = workspace.CurrentCamera:ViewportPointToRay(game:GetService("UserInputService"):GetMouseLocation().X, game:GetService("UserInputService"):GetMouseLocation().Y, 0)

	local res = workspace:Raycast(viewportResult.Origin, viewportResult.Direction.Unit * 500, Guncast)

	local currentRecticleColor = Color3.fromRGB(238, 238, 238)

	if res and res.Instance.Parent:FindFirstChild("Humanoid") and res.Instance.Parent.Humanoid.Health > 0 then

		if require(game:GetService("ReplicatedStorage").Modules.TeamRegistrations)[game.Players.LocalPlayer.Team.Name][game.Players:GetPlayerFromCharacter(res.Instance.Parent).Team.Name] then
			currentRecticleColor = Color3.fromRGB(162, 255, 148) --Ally
		else
			currentRecticleColor = Color3.fromRGB(161, 45, 45) --Enemy
		end

		newHighlight.FillColor = currentRecticleColor
		newHighlight.Parent = res.Instance.Parent --targetInfo.CharacterModel
		return
	end

	newHighlight.Parent = nil --If nothing or a part not under a character
end

local function Shoot()

	local newAlliedParams = RaycastParams.new() --Mousecast
	newAlliedParams.FilterType = Enum.RaycastFilterType.Exclude
	newAlliedParams.CollisionGroup = "Bullet"
	newAlliedParams.IgnoreWater = true
	newAlliedParams:AddToFilter(workspace.IgnoreandClearonEndorStart)

	for _,member in game.Players:GetPlayers() do
		if member.Character.Humanoid.Health > 0 and require(game:GetService("ReplicatedStorage").Modules.TeamRegistrations)[game.Players.LocalPlayer.Team.Name][member.Team.Name] then newAlliedParams:AddToFilter(member.Character)
		end	
	end

	local res = workspace:Raycast(script.Parent.Shoot.Position,(game.Players.LocalPlayer:GetMouse().Hit.Position - script.Parent.Shoot.Position).Unit*500, newAlliedParams)
	local intersection = res and res.Position or script.Parent.Shoot.Position + (game.Players.LocalPlayer:GetMouse().Hit.Position - script.Parent.Shoot.Position).Unit*500
	game:GetService("ReplicatedStorage").Remotes.SpecialEffects:FireServer("BulletVisual", 0.2, (script.Parent.Shoot.Position - intersection).Magnitude, script.Parent.Shoot.Position, intersection, Color3.new(1, 0.690196),0.2)

	if res and res.Instance.Parent:FindFirstChild("Humanoid") and res.Instance.Parent.Humanoid.Health > 0 then
		print("Enemy hit")
	end
end
1 Like

Is your problem that when shooting through an ally, the bullets go in the wrong direction? I’m not sure what you mean.

Yeah, should’ve posted a video showing it, here’s what I mean, sorry.

External Media

Oh, drat, 10mb limit, paste 0jpl6a into streamable if you don’t feel like downloading that mp4.

The enemies need to be included in the mouse TargetFilter. The raycasts aren’t swerving completely off, they’re just using the mouse’s position on the ally.

Got the raycast names muddled up. The first one is meant to be the cursor raycast and the one below that is the gun raycast.

I could be wrong, but I think you can only have one instance in a mouses targetfilter, so I made it the folder where every special effect is and what dead people are parented to. I could be misinterpreting you, but my issue is bullets swerving to varying degrees when specifically aimed at an ally.

This becomes a much bigger problem while in tight corridors during first person perspective, only the person at the very front will be able to 100% reliably hit their shots. I don’t think enemies have anything to do with this behaviour, they need to be detected by the mouse and gun raycast.

The problem only happens when aimed at an ally, aiming past them while in third person and at the enemy (when they are highlighted red) makes the rays pass through any allies seamlessly and straight to the enemy, always hitting them reliably other wise this is sometimes not the case.

Every ally is added to the gun raycast detection for exclusion. However, I don’t think Mouse.Hit detects this similar to mouse.target, so how would I replace the two parts which use this? The mouse raycast should already be excluding allies but it isn’t fully as you can tell by the swerving.

The raycast has allies excluded, but the mouse position doesn’t. That’s the problem. You’re correct that TargetFilter can only have one instance, so the only solution that I can see is to put all ignored objects (including allies) in the same parent folder. It sucks, but there’s no way around it that I know of.