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)