making a gun system, but i have this issue where the weapons doesn’t follow me when im moving, im currently using parabola for projectile system for it
local function fireBullet(origin, direction)
local parabola = Parabola.new()
parabola:setPhysicsLaunch(origin, direction * config.BULLET_SPEED, nil, 35 * -config.GRAVITY_FACTOR)
local bulletTemplate = BulletAssets:FindFirstChild("BulletTracer")
if not bulletTemplate then
warn("BulletTracer not found in ReplicatedStorage > GunAssets > BulletEffects")
return
end
local bullet = bulletTemplate:Clone()
bullet.Anchored = true
bullet.CanCollide = false
bullet.Position = origin
bullet.Parent = workspace
-- Orient bullet to face the direction it's traveling
local lookDirection = direction
bullet.CFrame = CFrame.lookAt(origin, origin + lookDirection)
local travelTime, maxTime = 0, config.MAX_DISTANCE / config.BULLET_SPEED
local lastPosition = origin
local conn
conn = RunService.Heartbeat:Connect(function(dt)
travelTime = travelTime + dt
if travelTime > maxTime then
conn:Disconnect()
bullet:Destroy()
return
end
local t0, t1 = (travelTime - dt) * config.BULLET_SPEED, travelTime * config.BULLET_SPEED
parabola:setDomain(t0, t1)
-- Check for hit first
local hitPart, hitPoint = parabola:findPart({player.Character})
if hitPart then
-- Position bullet at hit point
bullet.Position = hitPoint
-- Change color to indicate hit
bullet.BrickColor = BrickColor.Red()
-- Handle hit logic
local targetModel = hitPart:FindFirstAncestorOfClass("Model")
local humanoid = targetModel and targetModel:FindFirstChildOfClass("Humanoid")
if humanoid then
local isHeadshot = hitPart.Name == "Head"
remotes:WaitForChild("Hit"):FireServer(humanoid, hitPoint, hitPart.Name, isHeadshot)
end
conn:Disconnect()
-- Keep bullet visible briefly before destroying
task.delay(0.1, function()
if bullet and bullet.Parent then
bullet:Destroy()
end
end)
return
end
-- Update bullet position and orientation
local currentPos = parabola:samplePoint(1)
local travelDirection = (currentPos - lastPosition).Unit
-- Only update if we have a valid direction
if travelDirection.Magnitude > 0 then
bullet.CFrame = CFrame.lookAt(currentPos, currentPos + travelDirection)
else
bullet.Position = currentPos
end
-- Store current position for next frame
lastPosition = currentPos
end)
end