Need a bit help with bullet raycasting

Okay so i got a bit problem that my raycasting bullet inaccuratly spawning while shooting the gun. I tried to find out what is the problem of it but i didnt get solution of it
Video that shows the bullet ray spawn


Firing code. This is a half of entire code so i will show only main of raycasting

fireRemote.OnServerEvent:Connect(function(player, position)
	    local anim2 = Instance.new("Animation")
	    anim2.Name = "RecoilAnim"
	    anim2.AnimationId = "rbxassetid://"..settings["recoil id"]
	    local track2
	    track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
		track2.Priority = Enum.AnimationPriority.Action
		track2:Play()
		local origin = firePart.Position
		local direction = (position - origin).Unit*range
		local filter = RaycastParams.new()
		filter.FilterType = Enum.RaycastFilterType.Exclude
		filter.FilterDescendantsInstances = {tool.Parent,tool.Handle}
		filter.IgnoreWater = true
		local result = workspace:Raycast(origin, direction, filter)
      	local hit = result
		local intersection = result and result.Position or origin + direction
		local distance = (origin - intersection).Magnitude
		local bullet = Instance.new("Part",tool)
		bullet.Name = "Bullet"
		bullet.BrickColor = BrickColor.new("Bright yellow")
	    if settings["BCC"] == true then
		    local random = {"New Yeller","Bright red","Electric blue","Lime green","Plum","CGA brown","Dark stone grey","Lily white"} -- You can add here more colors
	    	bullet.BrickColor = BrickColor.new(random[math.random(1,#random)])
		end
		bullet.Transparency = 0.4
		bullet.Anchored = true
		bullet.CanCollide = false
		bullet.Locked = true
		bullet.Material = Enum.Material.Neon
		bullet.Size = Vector3.new(.1,.1,distance)
		bullet.Position = position
		bullet.CFrame = CFrame.new(intersection,origin)*CFrame.new(0, 0, -distance/2)
      	debrisService:AddItem(bullet, settings["bulletdestroytime"])