I’ve done this before. I used lerp
to somewhat create an “average deltatime”, which then I used that and multiplied it by the number in the CFrame alpha
. Here is the fixed code:
-- Services
local rS = game:GetService("RunService")
local tS = game:GetService("TweenService")
-- Variables
local camera = game.Workspace.CurrentCamera
local root = script.Parent:WaitForChild("HumanoidRootPart")
--local cameraPart = Instance.new("Part")
local newPos
-- Script
--[[cameraPart.Anchored = true
cameraPart.CanTouch = false
cameraPart.CanCollide = false]]
local offset = CFrame.new(0,10,30)
--cameraPart.Parent = game.Workspace
--cameraPart.CFrame = CFrame.lookAt(root.CFrame.Position + Vector3.new(0, 22, 30), root.Position)
camera.CameraType = Enum.CameraType.Scriptable
local avgdt = 1/60 --do not change
local adjustMultiplier = .001 --if you get stuttering, change the value
local smoothness = 10
function lerp(a, b, alpha)
return a + ((b - a) * alpha)
end
rS.RenderStepped:Connect(function(dt)
avgdt = lerp(avgdt, dt, adjustMultiplier)
local alpha = avgdt * 60 / smoothness
--cameraPart.Position = cameraPart.Position:Lerp(root.Position + Vector3.new(0, 22, 30), alpha)
camera.CFrame = camera.CFrame:Lerp(offset+root.Position, alpha)
--cameraPart.CFrame -- camera.CFrame = cameraPart.CFrame -- Set the camera's CFrame to the part's Cframe AFTER you have moved the part, so the camera gets moved correctly
end)
Unfortunately, this would seem to always have stuttering in your case. The other best solution would be to use a part with AlignPosition
to smoothly move to the root’s position. Here is the AlignPosition version:
-- Services
local rS = game:FindService("RunService")
--local tS = game:GetService("TweenService")
-- Variables
local camera = game.Workspace.CurrentCamera
local root = script.Parent:WaitForChild("HumanoidRootPart")
local cameraPart = Instance.new("Part")
local newPos
-- Script
local AP = Instance.new("AlignPosition")
local AT = Instance.new("Attachment")
local RootAT = Instance.new("Attachment")
RootAT.Parent = root
AT.Parent = cameraPart
AP.Attachment0 = AT
AP.Attachment1 = RootAT
AP.Responsiveness = 5 --smoothness from 5-200
--AP.MaxForce = cameraPart.Mass * workspace.Gravity * 2
--Unfortunately, MaxVelocity appears to be broken so it will not be used.
AP.Parent = cameraPart
cameraPart.Transparency = 1
cameraPart.CanTouch = false
cameraPart.CanCollide = false
cameraPart.CanQuery = false
--local offset = CFrame.new(0,10,30)
local offset = Vector3.new(0,22,30)
local angle = CFrame.Angles(-math.pi/4,0,0)
cameraPart.Parent = game.Workspace
--cameraPart.CFrame = CFrame.lookAt(root.CFrame.Position + Vector3.new(0, 22, 30), root.Position)
camera.CameraType = Enum.CameraType.Scriptable
--[[local avgdt = 1/60 --do not change
local adjustMultiplier = .001 --if you get stuttering, change the value
local smoothness = 10
function lerp(a, b, alpha)
return a + ((b - a) * alpha)
end]]
rS.RenderStepped:Connect(function(dt)
--[[avgdt = lerp(avgdt, dt, adjustMultiplier)
local alpha = avgdt * 60 / smoothness
--cameraPart.Position = cameraPart.Position:Lerp(root.Position + Vector3.new(0, 22, 30), alpha)
camera.CFrame = camera.CFrame:Lerp(offset+root.Position, alpha)
--cameraPart.CFrame -- camera.CFrame = cameraPart.CFrame -- Set the camera's CFrame to the part's Cframe AFTER you have moved the part, so the camera gets moved correctly]]
camera.CFrame = CFrame.new(cameraPart.Position + offset) * angle
end)
This eliminates the stuttering, but it causes other issues.