I’m working on a system that makes a bullet travel, but It ends early. I did some stuff to try to fix it, but I’m not sure exactly how?
local BULLET_SPEED = 1
local function onClientEvent(muzzle: Attachment, bulletIntersection: Vector3, bulletLength: number)
local travelDistance = (muzzle.WorldPosition - bulletIntersection).magnitude / 2
local bulletTracer = ReplicatedStorage.Particles.BulletTracer:Clone()
bulletTracer.CFrame = CFrame.lookAt(muzzle.WorldPosition, bulletIntersection)
bulletTracer.Parent = bulletsContainer
for index = 0, travelDistance, BULLET_SPEED do
if index ~= 0 then
bulletTracer.CFrame *= CFrame.new(0, 0, -BULLET_SPEED)
end
if (bulletTracer.Position - bulletIntersection).Magnitude < bulletTracer.Size.Z then
bulletTracer.Size = Vector3.new(bulletTracer.Size.X, bulletTracer.Size.Y, (bulletTracer.Position - bulletIntersection).Magnitude * 5)
bulletTracer.CFrame *= CFrame.new(0, 0, -bulletTracer.Size.Z / 10)
end
RunService.RenderStepped:Wait()
end
bulletTracer:Destroy()
end
Also why are you iterating over tbe magnitude? Woudlnt it be easier to just use tweens? Or if you want to add physics later use collectionService to make a bullet tag and then make it move by bullet speed in the direction of its lookVector? Then you can add th tag to each new bullet that spawns without all this nonsense.
When I remove the /2 part, there are bugs as shown above, the first one is that the bullet will appear* to go through walls and the second one is that when I fire too close the bullet doesnt show at all
I would’ve kept this one because it didn’t cause any other issues, but I really wanted to fix the fact that it ends shortly and I probably think I wasnt doing the tween part right
Its because you are iterating over the magnitude which is a FLOAT and the for loop iterates by default in whole INTEGERS to my knowledge so when you shoot close the Magnitude is like 0.87 and it iterates by 1 so it just doesn’t run same with the top pic the Magnitude is like 98.7 but it does 99 and it shoots through
Ah okay, since I believe the way I’m doing it right now is probably not the best like you said, is there any way I could solve it using tweens like you said?
I figured it out and it works decently, I just have to figure out some way to make it so that the size changes based on how close it is to the intersecting point so that it doesnt appear to go OVER the hit spot
local function onClientEvent(muzzle: Attachment, bulletIntersection: Vector3, bulletLength: number)
local bulletTracer = ReplicatedStorage.Particles.BulletTracer:Clone()
bulletTracer.CFrame = CFrame.lookAt(muzzle.WorldPosition, bulletIntersection)
bulletTracer.Parent = bulletsContainer
local t = TweenService:Create(bulletTracer, TweenInfo.new(bulletLength / 1000), {Position = bulletIntersection})
t:Play()
t.Completed:Connect(function()
bulletTracer:Destroy()
end)
end