Raycast is innacurate when simulated through excluded object

I have made a simple raycast script where the player can create a raycast using their mouse position. The raycast works when the mouse hit position is a normal part, but when it is an ignored part (using raycast parameters exclude option) it tends to be innacurate.

When the mouse hit position is the ignored part vs when it is a non ignored part through the ignored part:

GIF 5-10-2023 10-52-46 AM

Here is the code:

function Module.Raycast(OriginPosition, TargetPosition, IgnoredDescendants)
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParameters.FilterDescendantsInstances = IgnoredDescendants
	
	return workspace:Raycast(OriginPosition, TargetPosition, RaycastParameters)
end

function Module.Create(OriginPosition, TargetPosition, Projectile, IgnoredDescendants)--projectile will be used for wall plug thing
	local Direction = (TargetPosition - OriginPosition).Unit
	local Distance = (OriginPosition - TargetPosition).Magnitude * 2
	
	local RaycastResult = Module.Raycast(OriginPosition, Direction * Distance, IgnoredDescendants)
	
	if RaycastResult == nil then
		print("nil")
		
	else
		print(RaycastResult.Position)
		
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.CanQuery = false
		p.Color = Color3.fromRGB(255,0,0)
		p.Parent = workspace
		p.Position = RaycastResult.Position
		
	end
end

Position the part with the CFrame property instead of the Position property. Position will take into account collisions while CFrame will more accurately position parts where you want them to go, such as intersecting objects.

p.CFrame = CFrame.new(RaycastResult.Position)

That still yields the same result, I believe it is something to do with the raycast portion, possibly my direction and distance are innacurate or something

If the origin position is the player’s position, it would actually be accurate. Since the raycast ignores the transparent parts, the raycast is going to move past the given mouse position, which is at the ignored part, until it hits another part or doesn’t hit anything. You could probably create another part to visualize how this raycast works if you’re still sure that it’s inaccurate.