Trying to make a 2d camera with a somewhat delay effect, but when I use deltatime it lags after a while

So I’ve been trying to fix this issue for about a month, but it doesn’t seem to work, decided to use chatgbt to fix the issue but still didn’t work. The issue is that when you join the game, the camera starts out fine, but when you continue playing, the camera or framerate starts to lag, this only happens when I seem to try to add any form of deltatime into the camera script. This issue also amplifies on higher framerates.

The code:

local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")


local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")


local __init__ = script.Parent
local event = __init__.events.changeCamera


local cameraPart = RS:FindFirstChild("C"):Clone(workspace)
local camera = workspace.CurrentCamera
local camera_subject = root

local camera_x = 0
local camera_y = 0
local cameraSpeed = 0.5


local lastFrame = tick()
local min_Threshold = 0.1



--Function-Called-------------------------------------------------------------------------------


event.OnClientEvent:Connect(function(player, part)
	camera_subject = part
	camera.CFrame = camera_subject
end)


--OnFrame-------------------------------------------------------------------------------


local function onFrame()
	local deltaTime = tick() - lastFrame
	lastFrame = tick()
	
	local targetX = camera_subject.Position.X
	local targetY = camera_subject.Position.Y + 2
	
	local dx = targetX - camera_x
	local dy = targetY - camera_y
	
	if math.abs(dx) > min_Threshold or math.abs(dy) > min_Threshold then
		camera_x = camera_x + dx / cameraSpeed * deltaTime
		camera_y = camera_y + dy / cameraSpeed * deltaTime
	end

	cameraPart.Position = Vector3.new(camera_x, camera_y, 40)
	cameraPart.Orientation = Vector3.new(0,0,0)
	
	camera.CFrame = cameraPart.CFrame

	camera.CameraType = Enum.CameraType.Scriptable
	
	
	root.CFrame = CFrame.new(root.CFrame.X, root.CFrame.Y, 0)
end


---------------------------------------------------------------------------------

RunService.Heartbeat:Connect(onFrame)

Video of the issue:

thank you

3 Likes

RunService events like Heartbeat and RenderStepped will pass a delta time parameter in the connection, so you can do this, as an example:

local function onFrame(deltaTime: number)
    -- Your code to lerp the camera, which should use deltaTime
end

Other than that, your scope seems to be fine. If anything, it’d be nice to know if there’s anything else that’s conflicting with the camera’s movement.

1 Like

thanks but it still lags after a while

try using os.clock() instead of tick() or making an infinite loop with task.wait()

didn’t really help, I’m pretty sure the issue is a positional issue with the camera but I’m not really sure. it’s really bad when making sharp movements like moving the opposite direction or jumping.

Actually, I’m pretty sure it’s another script thats lagging the game, mainly the one that manages the animations of the 2d character. Because disabling that script allows my game to run smooth.

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