Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so the bullet actually comes from the barrel?

In my gun, if I stand in one spot and shoot, the bullet will come out from the barrel. If I start jumping around like an idiot with the gun, or going in circles super fast, the bullet comes out from where it was 1 second ago. How to fix? My codes.

Bullet:

function getRemote(remote)
	if remote:IsA("RemoteEvent") and remote.Name == "bulletReplicator" then
		remote.OnClientEvent:connect(fire)
	end
end

for _,remote in pairs(game.ReplicatedStorage:GetChildren()) do
	getRemote(remote)
end

game.ReplicatedStorage.ChildAdded:Connect(function(remote)
	getRemote(remote)
end)

local bulletStorage = Instance.new("Folder",workspace)

function fire(character,startPos,range,direction,gravity)
	local lastPos = startPos
	local startTime = tick()
	local tracer = getTracer()
	tracer.Position = startPos
	
	local rayFilter = rayFilter(character)
	while range > 0 do
		local timeLength = (tick()-startTime)
		local currentPos = startPos + (direction*timeLength)
		currentPos = applyGravity(currentPos,gravity,timeLength)
		local distance = (lastPos - currentPos).Magnitude
		range = range-distance

		local ray = workspace:Raycast(lastPos,currentPos-lastPos,rayFilter)
		if ray then
			local hit = ray.Instance
			currentPos = ray.Position
		end	
		updateTracer(tracer,lastPos,currentPos)
		if ray then
			break
		end
		lastPos = currentPos
		task.wait()
	end
	task.wait()
	tracer:Destroy()
end

function applyGravity(currentPos,gravity,timeLength)
	return currentPos+(gravity*timeLength^2)
end

function updateTracer(tracer,lastPos,rayPos)
	tracer.Size = Vector3.new(.1,.1,(lastPos-rayPos).Magnitude)
	tracer.CFrame = CFrame.new(rayPos,lastPos)*CFrame.new(0,0, -(lastPos-rayPos).Magnitude/2)
end

function rayFilter(character)
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {character,bulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist
	return rayFilter
end

function getTracer()
	local tracer = Instance.new("Part",bulletStorage)
	tracer.Anchored = true
	tracer.CanCollide = false
	tracer.Locked = true
	tracer.BrickColor = BrickColor.new("Gold")
	tracer.Material = Enum.Material.Neon
	return tracer
end

Mouse:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

equipped = false
script.Parent.Equipped:Connect(function()
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

function fireLoop()
	while firing and equipped do
		script.Parent.RemoteEvent:FireServer(mouse.Hit.p)
		task.wait(.01)
	end
	firing  = false
end
mouse.button1Down:connect(function()
	if equipped == false then
		return
	end
	
	local sound = Instance.new("Sound",script.Parent)
	sound.SoundId = "rbxassetid://410814187"
	sound:Play()
	sound.Ended:connect(function()
		sound:Destroy()
	end)
	
	mouse.button1Up:connect(function()
		firing = false
	end)
	if firing == true then return end
	firing = true
	fireLoop()
end)

local remoteEvent = script.Parent:WaitForChild("RemoteEvent",15)
remoteEvent.OnClientEvent:Connect(function()
	local hitMarker = Instance.new("Sound",script.Parent)
	hitMarker.SoundId = "rbxassetid://160432334"
	hitMarker:Play()
	hitMarker.Ended:connect(function()
		hitMarker:Destroy()
	end)
end)

1 answer

you need to set the origin of the gun tracer to the Barrel

This discussion is preserved as it was. New answers, comments, and votes are closed.