I’m trying to make a projectile system, but there is some inconsistency that I would like to remove
as shown above, these projectiles have the same starting values with Position, Velocity and Acceleration which is the world’s gravity. Despite that, these projectiles somehow do not always follow the same absolute path
Here is the code I use to calculate and step projectiles:
function reflect(lv, nv)
return lv - 2 * nv * (lv:Dot(nv))
end
function StepProjectiles(t, dt)
for key, projectile : Packet in ProjectileCache do
local timeNow = os.time()
-- local projectileInfo = Projectiles[projectile.ProjectileType]
CastParams.FilterDescendantsInstances = projectile.Blacklist
local steppedVelocity = projectile.Velocity + (projectile.Acceleration * dt)
local steppedPosition = projectile.Position + (steppedVelocity * dt)
print(steppedPosition)
local OG_Pos = projectile.Position
projectile.Velocity = steppedVelocity
projectile.Position = steppedPosition
projectile.Visual.Position = projectile.Position
local castResult = workspace:Raycast(OG_Pos , projectile.Position - OG_Pos, CastParams)
if castResult then
if table.find(projectile.TargetList,castResult.Instance:FindFirstAncestorWhichIsA('Model')) or castResult.Instance.Name == "_Barrier" or castResult.Instance.Parent.Name == "_Barrier" then
ProjectileCache[key] = nil
projectile.KeyEvent:Fire(castResult)
projectile.KeyEvent.Event:Wait()
projectile.KeyEvent:Destroy()
end
end
if timeNow - projectile.StartTime > projectile.Lifetime then
ProjectileCache[key] = nil
projectile.KeyEvent:Fire(nil)
projectile.KeyEvent.Event:Wait()
projectile.KeyEvent:Destroy()
return
end
if castResult and projectile.Bounce >= 1 then
projectile.Bounce -= 1
local ProjectileVector = (projectile.Position - castResult.Position).Unit
local ReflectedVector = reflect(ProjectileVector,castResult.Normal)
projectile.Position = castResult.Position
projectile.Velocity = ReflectedVector * projectile.Velocity.Magnitude
-- print(projectile.Position)
elseif castResult and projectile.Bounce < 1 then
-- print("Ended",castResult)
ProjectileCache[key] = nil
projectile.KeyEvent:Fire(castResult)
projectile.KeyEvent.Event:Wait()
projectile.KeyEvent:Destroy()
end
end
end
RunService.Stepped:Connect(StepProjectiles)
I would appreciate some advices