Help with manipulating gravity in fastcast for bullets

  1. What do you want to achieve? Keep it simple and clear!
    Manipulating gravity of a part using fastcast

  2. What is the issue? Include screenshots / videos if possible!
    It glitches after a few seconds of turning

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for solutions but none of them matched mine

Hi im new to scripting and i wanna achieve like a homing missile effect on bullets and the problem is that after a few seconds the bullet glitches

This is my code:

local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(game.ServerScriptService.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local player = script.Parent.Parent.Parent
local playercharacter = player.Character

local RNG = Random.new()

local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"

local bulletTemplate = game.ReplicatedStorage.Bullet

local caster = FastCast.new()

local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true

local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -10, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate

local function onEquipped()
	castParams.FilterDescendantsInstances = {tool.Parent, bulletsFolder}
end

local function terminated(cast)
	cast.RayInfo.CosmeticBulletObject:Destroy()
end

local function updateGravityForce(bullet)
	local target = game.Workspace.ENEMY_SIZES.Dummy.Torso
	local direction = (target.CFrame.Position - bullet.CFrame.Position).unit
	print(direction)
	return direction * 10
end

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	if bullet then 
		if cast.StateInfo.DistanceCovered <= 1000 then
			local updatedgrav = updateGravityForce(bullet)
			cast.StateInfo.Trajectories[1].Acceleration = updatedgrav
			local bulletLength = bullet.Size.Z/2
			local offset = CFrame.new(0, 0, -(length - bulletLength))
			bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
		else
			cast.Terminate(cast)
		end
	end
end

local function onRayHit(cast, result, velocity, bullet)
	local hit = result.Instance
	
	local character = hit:FindFirstAncestorWhichIsA("Model")
	if character and character:FindFirstChild("Humanoid") then
		character.Humanoid:TakeDamage(1)
	end
end

local function fire(player, mousePosition)
	if tool.Parent:IsA("Backpack") then return end
	local yes = true
	local origin = firePoint.WorldPosition
	local direction = CFrame.new(origin,mousePosition).LookVector * 500 + Vector3.new(RNG:NextNumber(-5,5),RNG:NextNumber(-5,5),RNG:NextNumber(-5,5))

	local activecast = caster:Fire(origin, direction, 20, castBehavior)
	local part = Instance.new("Part")
	part.CFrame = script.Parent.Body.Barrel.CFrame + script.Parent.Body.Barrel.CFrame.LookVector * 1
	part.Anchored = true
	part.Parent = game.Workspace.BulletFolder
	part.CanCollide = false
	part.Transparency = 1
	local attach = Instance.new("Attachment")
	attach.Parent = part
	local bem = Instance.new("Beam")
	bem.Parent = part
	bem.Attachment0 = attach
	bem.Attachment1 = activecast.RayInfo.CosmeticBulletObject:FindFirstChild("Attachment5")
	bem.Color = ColorSequence.new(Color3.fromRGB(0,255,0),Color3.fromRGB(0,255,0))
	bem.LightEmission = 1
	bem.Width0 = 0.2
	bem.Width1 = 0.2
	bem.Transparency = NumberSequence.new(0.5)
	bem.FaceCamera = true
	script.Parent.Handle.GunShot:Play()
	spawn(function()
		spawn(function()
			if next(activecast) ~= nil then
				repeat
					
					local thing = (activecast.RayInfo.CosmeticBulletObject.Position - script.Parent.Handle.FirePoint.Position).Magnitude >= 4
					
					if not thing then
						wait(0.0333)
					end	
					
				until thing
				
				activecast.RayInfo.CosmeticBulletObject:FindFirstChild("Trail").Transparency = NumberSequence.new(0.5)
				bem:Destroy()
				part:Destroy()
			end
		end)
		if yes == true then
			yes = false
			wait(10)
			if next(activecast) ~= nil then
				activecast.Terminate(activecast)
			end
		end
	end)
end

fireEvent.OnServerEvent:Connect(fire)
tool.Equipped:Connect(onEquipped)

caster.CastTerminating:Connect(terminated)
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(onRayHit)```

Thanks in advance!