Why is my ball so laggy?

Im using a bezier to make a ball travel to the net perfectly but it sometimes jitters.

Here is my code :

local PlayersService = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Player = PlayersService.LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid : Humanoid = Character:WaitForChild("Humanoid")

local Ball = workspace.Ball

local function lerp(p0: Vector3, p1: Vector3, t: number): Vector3
	return p0 * (1 - t) + p1 * t
end

local function quad(p0: Vector3, p1: Vector3, p2: Vector3, t: number): Vector3
	return lerp(lerp(p0, p1, t), lerp(p1, p2, t), t)
end

local function resetCamera()
	task.delay(1, function()
		workspace.CurrentCamera.CameraSubject = Humanoid
		Ball.Anchored = false
	end)
end

local function animateBall(P0: Vector3, P1: Vector3, End: Vector3, Duration: number)
	local Start = os.clock()
	local tD = task.wait()
	Ball.Anchored = true

	local Connection
	Connection = RunService.RenderStepped:Connect(function()
		local t = math.clamp((os.clock() - Start) / Duration, 0, 1)
		local newCFrame = CFrame.new(quad(P0, P1, End, t))

		workspace.CurrentCamera.CameraSubject = Ball
		Ball.CFrame = newCFrame * CFrame.Angles(0, math.rad(15), 0)
		if os.clock() - Start >= Duration then
			resetCamera()
			Connection:Disconnect()
			return
		end
	end)
end

local function Balle()
	local P0: Vector3 = Ball.Position
	local End: Vector3 = workspace.SoccerGoal.Target.Position

	local part = Instance.new("Part")
	part.Position = (P0 + End) / 2
	local distance = (End - P0).Magnitude
	local yOffset = distance / 2
	part.CFrame = part.CFrame * CFrame.new(0, yOffset, 0)

	local P1 : Vector3 = part.Position

	local tD = task.wait()
	local Duration = distance / 100

	animateBall(P0, P1, End, Duration)
end

UserInputService.InputEnded:Connect(function(input,gamePro)
	if gamePro then return end
	if input.KeyCode == Enum.KeyCode.E then
		Balle()
	end
end)
1 Like

Use BindToRenderStep instead, then tell me if it still jitters. To me the video looks fine so idk what you are talking about.

How is that any different from RenderStepped?

Heartbeat is responsible for physics simulation, try switching to it.

2 Likes

Okay I changed it but the Connection:Disconnect() doesn’t work now. How can I fix it?

When I changed it to the BindToRenderStep it works fine now but It doesnt disconnect the connection now

RunService:UnbindFromRenderStep(<name>), first argument you used when binding is the name.

Thanks a lot. I will give the solution to @RobloxRulesAbider because he was the first one to tell me the fix. Hope you are fine with that :wink:

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