Trying to make a lock on camera system, but it keeps glitching whenever i walk backwards
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:FindFirstChildOfClass("Humanoid")
local humRP = hum.RootPart
local otherHP = plr.PlayerGui.OtherHPBarGui
otherHPFrame = otherHP.HealthbarFrame.HealthBar
local uis = game:GetService("UserInputService")
hum:SetAttribute("Locked", true)
local function getTarget(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton3 then
local camera = workspace.CurrentCamera
local cameraCF = camera.CFrame
local camDefPos = cameraCF
local length = 66.6667
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local cameraCF = camera.CFrame
local raycastResult = workspace:Raycast(cameraCF.Position, cameraCF.LookVector * length, raycastParams)
if raycastResult.Instance.Parent:IsA("Model") then
local Target = raycastResult.Instance.Parent.Head
local targetModel = raycastResult.Instance.Parent
local targetHum = raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid")
local targetRP = targetHum.RootPart
local function stopCam()
game:GetService("RunService"):UnbindFromRenderStep("LockOn")
game:GetService("Debris"):AddItem(char:WaitForChild("Head"):WaitForChild("CamTarget"), .25)
game:GetService("Debris"):AddItem(char:WaitForChild("Head"):WaitForChild("CamWeld"), .25)
game:GetService("TweenService"):Create(camera, TweenInfo.new(.25, Enum.EasingStyle.Quad), {CFrame = CFrame.new(camDefPos.Position, char:WaitForChild("Head").Position)}):Play()
game:GetService("TweenService"):Create(camera, TweenInfo.new(.25, Enum.EasingStyle.Quad), {FieldOfView = 70}):Play()
camera.CameraType = Enum.CameraType.Custom
otherHP.Enabled = false
end
if hum:GetAttribute("Locked", true) then
print("alpha")
local camTarget = Instance.new("Part", char.Head)
camTarget.Name = "CamTarget"
camTarget.Size = Vector3.new(.5,.5,.5)
camTarget.Anchored = false
camTarget.Transparency = 1
camTarget.Massless = true
camTarget.CanCollide = false
camTarget.CanTouch = false
camTarget.CanQuery = false
camTarget.FrontSurface = Enum.SurfaceType.Hinge
local camWeld = Instance.new("Weld", char.Head)
camWeld.Name = "CamWeld"
camWeld.Part0 = char.Head
camWeld.Part1 = camTarget
camWeld.C0 = CFrame.new(3, 3.5, 5)
repeat
task.wait()
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 90
until
camera.CameraType == Enum.CameraType.Scriptable and camera.FieldOfView == 90
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
otherHP.Enabled = true
local function LockOn()
game:GetService("TweenService"):Create(camera, TweenInfo.new(.25, Enum.EasingStyle.Quad), {CFrame = CFrame.new(camTarget.Position, Target.Position)}):Play()
humRP.CFrame = CFrame.new(humRP.Position,Vector3.new(Target.Position.X, humRP.Position.Y, Target.Position.Z))
local health = targetHum.Health
local f = health/100
otherHP.Adornee = Target
otherHPFrame:TweenSize(UDim2.new(f,0,1,0),"Out","Quad",0.25)
end
game:GetService("RunService"):BindToRenderStep("LockOn", Enum.RenderPriority.Camera.Value - 1, LockOn)
hum:SetAttribute("Locked", false)
print("alpha")
elseif hum:GetAttribute("Locked") == false or hum.Died or targetHum.Died then
stopCam()
hum:SetAttribute("Locked", true)
end
else
print("nah")
end
end
end
uis.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
getTarget(input, gameProcessedEvent)
end)