Bullet is spawning away from the front of barrel

Hello. I’m trying out a simple bullet script and there seems to be an offset when my bullet spawns. That is, the bullet spawns a few studs in front of the barrel (when it should spawn in its exact position) as the constant that I’m multiplying to the velocity variable increases (I need it high to get it to travel faster and further) and is projected smoothly from there to its target position.

video:
https://gyazo.com/e21d40c384e7acff249e0e184528cace

local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")

local bullet = {}

function bullet.Cast(face, target)
	
	local initialPos = face.CFrame
	
	local Bullet = Instance.new("Part")
	Bullet.Parent = game.Workspace
	Bullet.Size = Vector3.new(0.26, 0.2, 0.53)
	Bullet.CFrame = initialPos
	Bullet.Anchored = true
	Bullet.CanCollide = false

	local a1, a2 = Instance.new("Attachment", Bullet), Instance.new("Attachment", Bullet)
	a2.CFrame = a2.CFrame * CFrame.new(.2, .3, 0)
	local trail = Instance.new("Trail", Bullet)
	trail.Lifetime = 55
	trail.Attachment0 = a1
	trail.Attachment1 = a2
	
	local velocity = CFrame.new(Bullet.Position, target).LookVector * 1500

	local fireBullet = RunService.RenderStepped:Connect(function(deltaTime)
		
		velocity = velocity - (Vector3.new(0, workspace.Gravity, 0) * deltaTime) 
		
		local newPos = initialPos + velocity * deltaTime
		
		Bullet.CFrame = newPos
		
		initialPos = newPos
		
	end)
	
	
end


return bullet

Can’t seem to figure out a way to fix it without it getting worst.
Thanks!

1 Like

Fixed it. Made the newposition of the bullet start at the initial position outside the runservice function.

local velocity = CFrame.new(Bullet.Position, target).LookVector * 1500
	
	local initialPos = face.CFrame
	local newPos = initialPos
	
	local fireBullet
	
	fireBullet = RunService.RenderStepped:Connect(function(deltaTime)
		
		velocity = velocity - (Vector3.new(0, workspace.Gravity, 0) * deltaTime) 
		
		Bullet.CFrame = newPos
		
		newPos = initialPos + velocity * deltaTime
	
		initialPos = newPos
		
		print(Bullet.Position.Magnitude)
		
	end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.