So I’ve been working on a gun system, except theres one big problem. The bullets are way off whenever you shoot while moving. I’ve had this problem in the past with an old gun system I made, and I easily fixed it by just making the bullets appear on the client only. I tried that again this time and it made no difference. Here is my code:
Server:
function shoot(gun, pos)
if not gun:FindFirstChild("SpecialShoot") then
local origin = gun.Barrel.Tip.WorldPosition
local direction = (pos.p - origin).Unit * gun.GunConfig.Range.Value
local result = workspace:Raycast(origin, direction)
game.ReplicatedStorage.Remotes.BulletReplicate:FireAllClients(origin, direction)
gun.BodyAttach.Shoot:Play()
else
require(gun.SpecialShoot).Fire(pos)
end
end
game.ReplicatedStorage.Remotes.GunEvent.OnServerEvent:Connect(function(plr, gun, event, pos)
if event == "Shoot" then
shoot(gun, pos)
end
end)
Client bullet script:
function createBullet(origin, direction)
local midpoint = origin + direction / 2
local b = Instance.new("Part", workspace)
b.Anchored = true
b.CanCollide = false
b.Material = Enum.Material.Neon
b.BrickColor = BrickColor.new("New Yeller")
b.CFrame = CFrame.new(midpoint, origin)
b.Size = Vector3.new(.1, .1, direction.magnitude)
game:GetService("Debris"):AddItem(b, 0.1)
end
game.ReplicatedStorage.Remotes.BulletReplicate.OnClientEvent:Connect(createBullet)
This is what it looks like:
Does anyone know why this is happening or how to fix it?