Lock on camera system glitching

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)
2 Likes

I’m not exactly sure what could be the cause of the glitchy behaviour but setting the camera.CameraType to Enum.CameraType.Scriptable whenever the lock on occurs seemed to fix it.

Btw you can replace the camTarget.Position at the tween creation to char.Head.CFrame * Vector3(3, 3.5, 5). It should be the same as welding a part to the head and getting said parts position, unless you need the camTarget part for something else as well.
Multiplying a CFrame with a Vector3 moves a point in the CFrame’s local space by the Vector3 and returns the point’s position in world space.

3 Likes

thanks for the second part but setting the camera type to scriptable didnt fix anything

1 Like

Oh I didn’t see you were already setting the CameraType to Scriptable.

Now that I test out the code snippet it seems to work well without any glitches for me.
Is there another script that’s setting the CameraType?
Also try printing the CameraType in the LockOn function.

1 Like

i tried all the camera types and it happens on all of them.
also, it does print the desired camera type

imma try following the humanoidrootpart next

EDIT: it did nothing

Ohh seems like you just have to first set the root part CFrame and only then start the camera tweening. I guess it makes sense since rn the scripts essentially using the last frame’s root part CFrame for updating the camera.

1 Like

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