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)