Bullet ends before it travels to the target fully

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

The blue part is when the bullet stops (or gets deleted), the red part is where it SHOULD stop

1 Like

You divide travel distance by 2 and it ends half the way on the screenshot, try removing the divide by 2.

2 Likes

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.

2 Likes



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

1 Like

This was my original code:

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), {CFrame = bulletTracer.CFrame * CFrame.new(0, 0, -bulletLength / 2)})
	t:Play()
	task.delay(bulletLength / 1000, bulletTracer.Destroy, bulletTracer)
end

The problem was that it also had that same issue, but I might’ve not done it right, so im not sure

1 Like

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

1 Like

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

2 Likes

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?

1 Like

To solve it just use a tween to animate the bullet flying from the muzzle to the hit position, or Use a LERP as they are more responsive overall.

1 Like

I dont really know what look you wanna achieve but you can just Tween the position of the bullet after orienting it to point towards the hit position.

Or alternatively learn to use CollectionService and make separate code for the bullet flying forward and separate code for spawning the bullets.

I cant provide code now because im on phone but i will try to comeback here soon with an example.

2 Likes

Also for this dont use task.delay instead Connect the code to the Completed event from the Tween. or do :Wait at the end of the Completed.

local t = game:GetService("TweenService"):Create(datahere)

t.Completed:Connect(function()

end)
2 Likes

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

I’ll try to experiment to fix it

1 Like

Its because the position is from the center of the part you need to offset it by the half of the size along the LookVector (Z Axis)

1 Like
local function onClientEvent(muzzle: Attachment, bulletIntersection: Vector3, bulletLength: number)
	local bulletTracer = ReplicatedStorage.Particles.BulletTracer:Clone()
	bulletTracer.CFrame = CFrame.lookAt(muzzle.WorldPosition, bulletIntersection) * CFrame.new(0, 0, -bulletTracer.Size.Z / 2)
	bulletTracer.Parent = bulletsContainer
	task.wait(0.001)
	local t = TweenService:Create(bulletTracer, TweenInfo.new(bulletLength / 50), {Position = bulletIntersection})
	t:Play()
	t.Completed:Connect(function()
		bulletTracer:Destroy()
	end)
end

Okay, I think this fixed it, I’ll mark a solution after some tests

2 Likes

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