I have an NPC that shoots a projectile with a loop and I want to change this into ApplyImpuse so it can reduce lag. How can I achieve this?
Code:
Remote.Event:Connect(function(firePoint, hitPosition)
local g = Vector3.new(0,-100,0) -- gravity
local x0 = firePoint.WorldPosition -- position to shoot
local t = 1 -- time to destination
local v0 = (hitPosition - x0 - 0.5*g*t*t)/t
local projectile = Instance.new("Part")
projectile.CanCollide = false
projectile.Parent = HeroAbilitiesFolder
local nt = 0
local connection = RunService.Heartbeat:Connect(function()
projectile.CFrame = CFrame.new(0.5*g*nt*nt + v0*nt + x0, projectile.PrimaryPart.AssemblyLinearVelocity + projectile.PrimaryPart.Position)
nt = nt + RunService.Heartbeat:Wait()
end)
end)