So currently this trajectory script is currently way too laggy. Is there any way I can make it less laggy?
I stopped rendering the bullets, it only calculates the trajectory. Assume firerate of 1500 out of about 400 guns.
GRAVITY = game.Workspace.Gravity
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")
function createlaser(start,finish,bullet)
local length = (finish-start).Magnitude
local lookvector = (finish-start).Unit
--bullet.Size = Vector3.new(.175,.175,length)
bullet.Size = Vector3.new(.4,.4,length)
bullet.CFrame = CFrame.new(lookvector * length/2 + start)
bullet.CFrame = CFrame.new(bullet.Position, start + lookvector * length)
end
function Arc(t,lookvector,orgin,speed)
local Spin = CFrame.new(orgin,lookvector * 5 + orgin)
local Xdegree, Ydegree, Zdegree = Spin:ToOrientation()
local FireAngle = Xdegree -- angle up and down
local theta = Ydegree -- angle around y axis
local XDisplacement = t*math.cos(FireAngle)*speed
local YDisplacement = t*math.sin(FireAngle)* speed - t^2*GRAVITY/2
return Vector3.new(-math.sin(theta)*XDisplacement,YDisplacement,-math.cos(theta)*XDisplacement) + orgin
end
function DepthCheck(orgin,lookvector,object)
orgin = lookvector * 20 + orgin -- go forward 20 studs
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {object}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
local hitscan = workspace:Raycast(orgin,lookvector * -30,raycastParams)
if hitscan then
return hitscan.Position
end
end
local Trajectory = {}
function Trajectory.move(start,lookvector,speed,penetration)
--local bullet = game.ReplicatedStorage.Assets.Projectiles.Bullet:Clone()
--bullet.Parent = game.Workspace.Projectiles
local currentposition = start
local startTick = tick()
local t = 0
local avoid = {game.Workspace.Projectiles}
local distanceTraveled = 0
while penetration > 0 do
t = tick() - startTick
local IterationOrgin = currentposition -- This is the position that is the start of the iteration
--Debris:AddItem(bullet,3)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = avoid
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local finish = Arc(t,lookvector,start,speed) -- calculate the arc
local hitscan = workspace:Raycast(currentposition,(finish-currentposition).Magnitude*(finish-currentposition).Unit,raycastParams)
--createlaser(currentposition,finish,BrickColor.Red(),false)
if hitscan then
if hitscan.Instance.Parent:FindFirstChild("Humanoid") then
hitscan.Instance.Parent.Humanoid:TakeDamage(10)
table.insert(avoid,#avoid + 1,hitscan.Instance.Parent)
currentposition = hitscan.Position
else
local Hit = DepthCheck(hitscan.Position,(finish-currentposition).Unit,hitscan.Instance)-- checks to see if bullet can penetrate
if Hit then
currentposition = hitscan.Position
penetration = penetration - (currentposition-Hit).Magnitude -- subtract the depth
currentposition = Hit
else
penetration = 0
end
end
else
currentposition = finish
end
if t > 5 then -- after 5 seconds remove the bullet
penetration = 0
end
distanceTraveled = distanceTraveled + (currentposition - IterationOrgin).Magnitude -- move forward
--createlaser(IterationOrgin,finish,bullet)
RunService.Heartbeat:Wait()
end
--Debris:AddItem(bullet,0)
end
return Trajectory