Basketball cross-dribbling

Have this ‘cross dribble’ script which lerps the ball to middle position, then to the other hand back in the exact same way inside heartbeat. It works fine if the player stays still but if the player starts moving then it follows the player around and goes behind. I do know it’s because of the lerping but don’t know what would be the fix for it.

Here is my code

local currentOffset = character.HumanoidRootPart.CFrame:ToObjectSpace(ball.CFrame)

	crossDribbleConnection = RunService.Heartbeat:Connect(function(t)
		local groundObj = workspace:Raycast(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.UpVector * -100, raycastFilter)
		
		if groundObj then
			local groundHeight = groundObj.Position.Y

			if on then
				alpha = alpha + 0.015
				
				if alpha <= 0.2 then
					local rightHandPos = character.RightHand.Position
					
					local midPos = (character.RightHand.Position + character.LeftHand.Position)/2
					
					local currentPosition = character.HumanoidRootPart.CFrame * currentOffset
					
					ball.CFrame = currentPosition
					
					local targetPosition = Vector3.new(midPos.X , groundHeight, midPos.Z)
					ball.Position = currentPosition.Position:Lerp(targetPosition, alpha)
					
					currentOffset = character.HumanoidRootPart.CFrame:ToObjectSpace(ball.CFrame)
				else
					local currentPosition = character.HumanoidRootPart.CFrame * currentOffset

					ball.CFrame = currentPosition

					local targetPosition = character.RightHand.Position
					
					ball.Position = currentPosition.Position:Lerp(targetPosition, alpha)---0.2)
					currentOffset = character.HumanoidRootPart.CFrame:ToObjectSpace(ball.CFrame)

					if alpha >= 0.4 then
						on = false
						alpha = 0
					end
				end
			else
				alpha = alpha + 0.015
				if alpha <= 0.2 then
					local rightHandPos = character.RightHand.Position
					local midPos = (character.RightHand.Position + character.LeftHand.Position)/2
					
					local currentPosition = character.HumanoidRootPart.CFrame * currentOffset
					ball.CFrame = currentPosition

					local targetPosition = Vector3.new(midPos.X , groundHeight, midPos.Z)
					ball.Position = currentPosition.Position:Lerp(targetPosition, alpha)
					
					currentOffset = character.HumanoidRootPart.CFrame:ToObjectSpace(ball.CFrame)
				else
					local currentPosition = character.HumanoidRootPart.CFrame * currentOffset
					ball.CFrame = currentPosition

					local targetPosition = character.LeftHand.Position
					ball.Position = currentPosition.Position:Lerp(targetPosition, alpha-0.2)
					
					currentOffset = ball.CFrame:ToObjectSpace(character.HumanoidRootPart.CFrame)

					if alpha >= 0.4 then
						on = true
						alpha = 0
					end
				end
			
			end
		end
	end)
1 Like

Bump

1 Like

Maybe its useful to post the code that is related to your issue?
Just an idea though.

1 Like

Will post it if someone deems it to be necessary, didn’t believe it was necessary because the problem is with the concept.

Well i do believe its necessary, just because the code involves the lerping which you think is the issue.
We can’t help without code

2 Likes

I have edited the post and attached my code up there now

1 Like

Bump