Lock on system laggy whenever moving

I have made a simple lock on system but i noticed while using it that the camera was very choppy and kept almost teleporting

ive tried many things like switching to tween, using my current method and also trying a scriptable camera but i couldnt quite get them to work

Camera Stuttering

Code used to lock on
for reference i dont use auto rotate, instead i have it coded to be almost like a tween so the turning isnt so rapid and it can be disabled with a attribute i added to the player


-- variables used just for the visuals like highlight and the indicator
local lock = game.ReplicatedStorage.Lock:Clone()
local LH = game.ReplicatedStorage.LockHighlight:Clone()
lock.Parent = game.ReplicatedStorage
LH.Parent = game.ReplicatedStorage
lock.Adornee = nil
LH.Adornee = nil
local locktarget = nil
-- Player Variables
local plyr = game:GetService("Players").LocalPlayer
local Character = plyr.Character or plyr.CharacterAdded:Wait()
local mouse = plyr:GetMouse()
mouse.TargetFilter = Character
local cam = workspace.CurrentCamera
local hrp = chr:WaitForChild("HumanoidRootPart")
local hum = chr:WaitForChild("Humanoid")

local function checdist(thrp)
	local Mag = ( hrp.Position - thrp.Position).Magnitude
	if Mag < 50 then
		return true
	end
	return false
end

local function findnearesttarget()
	local targetdist = 50
	local target = nil
	local mousepos = mouse.Hit.Position
	for i,v in pairs(workspace:GetDescendants()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") and v~= Character then
			local targethrp = v.HumanoidRootPart
			local Mag = ( mousepos - targethrp.Position).Magnitude
			if Mag < targetdist then
				targetdist = Mag
				target = v
			end
		end
	end
	if target then
		locktarget = target
	end
end




UIS.InputBegan:Connect(function(key,gpe)
	if gpe == false then
		if key.KeyCode == Enum.KeyCode.L then
			if locktarget then
				locktarget = nil

			else
				findnearesttarget()
			end
		end
	end
end)


local lerpamount = 0.3

game:GetService("RunService").RenderStepped:Connect(function()
	wait()
	
	if locktarget then

		local TChar = locktarget
		local THRP = TChar:FindFirstChild("HumanoidRootPart")
		local THUM = TChar:FindFirstChild("Humanoid")

		if TChar and THRP and THUM then
			if THUM.Health <= 0 or checdist(THRP) == false then locktarget = nil return end
			lock.Parent = TChar
			LH.Parent = TChar
			lock.Adornee = THRP
			LH.Adornee = TChar



			local Point = THRP.Position

			local startedtime = os.clock()
			local tbl = {
				[hrp] = {["CFrame"] = CFrame.lookAt(hrp.CFrame.Position, THRP.Position)},
				[cam] = {["CFrame"] = CFrame.lookAt(cam.CFrame.Position, THRP.Position)},
			}

			for i,v in pairs(tbl) do

				local Deltatime = os.clock() - startedtime
				local lerpp = (1-lerpamount)^(Deltatime)
				local dir = i.CFrame:lerp(v["CFrame"], lerpp)

				i.CFrame = CFrame.fromMatrix(hrp.Position, dir.XVector, hrp.CFrame.YVector)
			end

			

		end

	else
		
		lock.Parent = game.ReplicatedStorage
		LH.Parent = game.ReplicatedStorage
		lock.Adornee = nil
		LH.Adornee = nil
		if plyr:GetAttribute("Locke") == false then
			
			local RootPos, MousePos = Root.Position, Mouse.Hit.Position
			local PositionTo = Vector3.new(MousePos.X, RootPos.Y, MousePos.Z)
			local tbl = {
				[hrp] = {["CFrame"] = CFrame.new(hrp.Position, PositionTo)},
			}

			local startedtime = os.clock()
			for i,v in pairs(tbl) do
				local Deltatime = os.clock() - startedtime
				local lerpp = (1-lerpamount)^(Deltatime)
				local dir = i.CFrame:lerp(v["CFrame"], lerpp)
				i.CFrame = dir
			end


		end
		
		
		
		
	end
end)

your using Render Stepped which will severely increase lag. Aswell as your using a for i loop in that. Don’t have enough time to think of a solution rn sorry.

I don’t think you need to insert “wait()” as RenderStepped run every frame the reason why it cause stutter might be it updated slower a few frame due to wait() resulting in update overlapping with old position (I am not very good at explaining but you could try remove it and see if it solve)

The wait() definitely isn’t needed there. The resumption time of threads yielded by wait() isn’t guaranteed which can lead to stuttering and it will cause a delay before changes take place which you don’t want in a camera script. I can’t tell if that is the sole issue but it is definitely a contributor.

Other things to note is that RenderStepped gives you the deltatime as the first argument in the connection so you don’t have to calculate it yourself.

I am also pretty sure the lerp formula you want is 1 - (1 - x)^dt, the formula you currently have would have visual differences on different framerates.

RenderStepped was the correct choice for this since you’re working with cameras and need immediate updating.

The reason why you should avoid it is because the engine will wait for all RenderStepped code to complete before continuing which can drop your FPS if you run expensive code in it more so than Stepped or Heartbeat.

In this case though since you need immediate updating of the camera and the code needed to do so won’t cause a drop to your FPS it is a good choice.

This worked great! adding the 1 - to the formula fixed it completely. Thank you for your response

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