Help with Bullet Calculations

Hello!
I’m having some issues with shooting bullets and where they’re supposed to be going.
right now there is no bullet drop, nor is there any spread, so it should eventually hit where my cursor is. This shouldn’t happen instantly though as it uses tween service to move the bullet, and raycasting to detect hits.
image

The yellow part on the wall is the bullet

local velocity = weaponData.BulletVelocity
		local delta, lastTick = 0,tick()
		local endPosition = mouse.Hit.p
		local currentPosition = tool.Hole.Position
		local startPosition = currentPosition
		bullet.Position = currentPosition
	
		local increase;
		local coveredDistance = 0
		local direction = CFrame.new(startPosition, endPosition).LookVector 
		local ray;
		local goal = {Position = nil}
		while wait() and (ray == nil) do
			delta = tick() - lastTick
			lastTick = tick()
			increase = delta * velocity
			coveredDistance = coveredDistance + increase
			if(coveredDistance > 1000)then
				warn("Okay let's cancel out of this")
				break
			end
			--ray between two points
			BulletParameters.FilterDescendantsInstances = {
				player.Character,
			}
			ray = workspace:Raycast(currentPosition, direction * increase, BulletParameters)
			if(ray)then
				warn("we hit an object",ray.Instance)
				currentPosition = ray.Position
				bullet.Anchored = true
			else
				currentPosition = currentPosition + Vector3.new(0,0,-increase)
				print(currentPosition)
			end
			
			goal.Position = currentPosition
			BulletTweenInfo = TweenInfo.new(
				(bullet.Position - currentPosition).magnitude * 1/velocity,--distance per sec, how long until update
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.Out,
				0,
				false,
				0
			)
			TweenService:Create(bullet, BulletTweenInfo, goal):Play()
		end

When i’m close to the wall or am aiming at a certain spot, the bullet is properly tween’d to my cursor’s hit, but othertimes it’s off, usually by a big margin. I’m not throwing any errors in my code.
I’m fairly new to this type of scripting as my other gun systems were just laser-gun implementations
Any advice or possible fixes will help, thanks :slight_smile:

1 Like