How to make basketball shooting smoother?

The ball does this weird freeze after 1 second, it just floats in the air and then after a few milliseconds starts the do its normal physics.

How can I get rid of the freeze?

local T = 1
local X0 = player.Character.HumanoidRootPart.CFrame * Vector3.new(0, 2, -2)
	local Gravity = Vector3.new(0, -workspace.Gravity, 0)
	local V0 = (workspace.BucketConfirm.Position - X0 - 0.5 * Gravity * T * T) / T
	
	local offset = Vector3.new(0, 0, math.clamp((100 - fillPercent) / 10, 0, 10))

	local NT = 0
	local quarterTime = T * 2 / 4

	if ball.Anchored then
		ball.Anchored = false
	end

	ball.Cooldown.Value = true
	playerWithBall = nil

	while (NT < T * 2) do
		ball.CFrame = CFrame.new(0.5 * Gravity * NT * NT + V0 * NT + X0 + offset) 
		if NT >= T then
			ball.AssemblyLinearVelocity = Vector3.zero
			ball.AssemblyAngularVelocity = Vector3.zero
			ball.CanCollide = true
			break
		end
		NT = NT + RunService.Heartbeat:Wait() * 0.5
	end

If it helps, similar to this: Modeling a projectile’s motion - Resources / Community Tutorials - Developer Forum | Roblox

I believe the ‘freezing’ is happening as the physics of the basketball get transferred from the player to the server. Objects close the character have their physics owned by that player, and as it goes further away it will transfer to the server. Try adding this and see if it helps:

ball:SetNetworkOwner(nil)
--Sets the network owner of the ball to the server
1 Like

Stop handling velocity this way. A simple, easy way to control shooting with the same tutorial is to do this:

--Disconnect the Basketball from any Motor6D used for animating prior to running this

local Gravity = Vector3.new(0, -workspace.Gravity, 0);
local EndPos = Goal.Position
local StartPos = Basketball.Position

local Dist = (EndPos - StartPos).Magnitude
local Speed = 65 -- Measured in studs per second. Higher = lower arc & vice versa
local Time = 0.5 + (Dist/Speed) -- Can be replaced by a simple number if you want a constant time

local BallVel = (EndPos - StartPos - 0.5*Gravity*Time*Time)/Time
Basketball:SetNetworkOwner(nil)
Basketball.CanCollide = false
Basketball.Velocity = BallVel

--// Re-enable basketball collisions after a brief period of time.
task.delay(.15, function()
       Basketball.CanCollide = true
end)

Notes:

  • The ball will freeze for a very short moment when releasing due to the NetworkOwner change. This is mostly unnoticeable, and it can be masked through more advanced handling of the ball’s physics.
  • The ball should never be anchored, especially if attached to a player via Motor6D. I’m unsure why you included an anchored check in your code.

Changes:

  • The Ball’s velocity is now applied once and not in a loop. CFrames on the server will cause large stuttering with shooting this way.
  • Relying on .Velocity is a more appropriate manner of moving the ball around.
2 Likes

Thank you @synical4 and @RiccoMiller for helping!
They both helped make it smoother.

1 Like