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!