Raycast only works at some angles?

I want the piercing in my gun to actually function properly, however when i shoot at only certain angles it registers and pierces the zombie. It doesn’t seem to pierce at all on other angles.
I couldn’t figure out a solution yet so far.

Here is the part of the code that handles the piercing

	if script.Parent.Ammo.Value > 0 then
			if script.Parent.Config.Burst.Value == false and script.Parent.Config.Spread.Value == false then
				script.Parent.Ammo.Value -= 1
			end		
			
			local firesound = script.Parent.Handle.Fire:Clone()
			firesound.Parent = script.Parent.Handle
			firesound:Play()
			
			local shellsound = script.Parent.Handle.playshell:Clone()
			shellsound.Parent = script.Parent.Handle
			shellsound.Disabled = false
			
			game.Debris:AddItem(firesound, 3)
			game.Debris:AddItem(shellsound, 4)

			local rayprm = RaycastParams.new()
			rayprm.FilterDescendantsInstances = {plr.Character, workspace.Projectiles}
			rayprm.FilterType = Enum.RaycastFilterType.Exclude

			local result = workspace:Raycast(script.Parent.Handle.Position, (mousepos + pos-script.Parent.Handle.Position).Unit*800, rayprm)

			if result then
				local hit = result.Instance

				if hit.Parent:FindFirstChild("Enemy") then
					local hitsound = script.Parent.Handle.Hit:Clone()
					hitsound.Parent = script.Parent.Handle
					hitsound:Play()

					game.Debris:AddItem(hitsound, 2)

					if hit.Name ~= "Head" then
						hit.Parent:FindFirstChild("Enemy"):TakeDamage(script.Parent.Config.dmg.Value)
					else
						hit.Parent:FindFirstChild("Enemy"):TakeDamage(script.Parent.Config.dmg.Value+10)
					end
					
					if script.Parent.Config.Pierce.Value == true then
						local forward = CFrame.new(result.Position).LookVector*1000
						local pierceray = RaycastParams.new()
						pierceray.FilterDescendantsInstances = {plr.Character, hit:FindFirstAncestorOfClass("Model"), workspace.Projectiles}
						pierceray.FilterType = Enum.RaycastFilterType.Exclude
						
						local pierceresult = workspace:Raycast(result.Position, forward, pierceray)
						
						if pierceresult then
							local piercehit = pierceresult.Instance
							print(piercehit.Name)
							if piercehit.Parent:FindFirstChild("Enemy") then
								print("pierced")

								local newhitsound = script.Parent.Handle.Hit:Clone()
								newhitsound.Parent = script.Parent.Handle
								newhitsound:Play()

								game.Debris:AddItem(newhitsound, 2)

								if piercehit.Name ~= "Head" then
									piercehit.Parent:FindFirstChild("Enemy"):TakeDamage(script.Parent.Config.dmg.Value)
								else
									piercehit.Parent:FindFirstChild("Enemy"):TakeDamage(script.Parent.Config.dmg.Value+10)
								end
							end
							
						else
							print("not pierced")
						end
					end
				end
			end
		else
			script.Parent.Handle.Empty:Play()
		end
1 Like

If you move a part to forward-result.Position, is it where you expect it to be?

If it works sometimes, the if pierceresult then code should be fine and chances are your destination (forward-result.Position) isn’t where you expect it to be. Could also be the origin (result.Position) but without the code for either I can’t really suggest much more.

yeah i think the problem really lays somewhere in the destination or origin itself

wait let me update the script to include the rest of the code

ok i have updated the script to include the rest of the fire code