Blacklisted/Excluded parts cause ray to be inaccurate

With a blacklisted part in the way (doesn’t shoot straight)
image

Without the part (shoots straight)
image

With the part but no longer blacklisted (shoots straight)
image

Does anyone know why this is?

1 Like

Are you using player:GetMouse()?

These should be identical, because if the part is blacklisted/excluded, the ray should act like it does not exist. I can only assume whatever code you are using to point the bullet in the correct direction does not use the same raycast parameters, but I need the code to say for certain.

Is the origin is the barrel of the gun? Try to set the origin to camera.Focus.Position

This is the raycast codee

function Raycast(Position, Mouse)
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character, script.Parent, workspace.Ignored, workspace.Terrain}
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.IgnoreWater = true
	local ray = Ray.new(Position, (Mouse - Position).Unit * Config.Range)
	local result = workspace:Raycast(ray.Origin, ray.Direction, rayParams) or (ray.Origin + ray.Direction)
	return result
end

Yes, I use mouse.Hit.p for the bullet

function GetMouse(Distance, castParams)
	local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
	local UnitRay = workspace.CurrentCamera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y)
	
	local origin = UnitRay.Origin
	local endp = UnitRay.Direction * Distance
	local hit = workspace:Raycast(origin, endp, castParams)
	
	if hit then
		return hit.Position
	else
		return UnitRay.Origin + UnitRay.Direction * Distance
	end
end

I think this would be better. I had encountered a similar problem a long time ago, so I thought this would fix the issue. Use the rayParams you made in your raycast as a castParams from GetMouse()

2 Likes

Thank you for this, I didn’t use this solution but this helped me realise it was actually the mouse.hit.p returning an incorrect value because of the part being in the way and I solved it instead using mouse.TargetFilter

1 Like

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