Why the freakout? (camera lerp)

  1. I want to achieve: a quick targeting push to the camera when lockon is pressed. This is not continuous.

  2. and 3. I am doing this by lerp because i couldnt get the tween to work out. but when i use my camera during targeting or switching targets theres a high chance of the camera freaking out entirely and the lerp completing hundreds of a time in a row. I tried setting the camera to scriptable, didnt help. tried to do an ‘enabled’ check (setting to false then to true before calling the function in the event). none of that works. relevant context is that i’ve turned cameratoggle on.

I really do not know what’s going on… im tired ha…i’ve tried this three different ways and it still won’t work. it seems like such a simple thing…

edit: i’ve changed around the script a bit and now it doesnt freak out but the camera does keep turning and turning around the avata gentlyr. ive been printing a bit and the alpha keeps climbing the more i’ve fired the lockon event…

local TWEEN_SERVICE = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
startPos = nil
endPos = nil
local runservice = game:GetService("RunService")

--[[Folders]]
local EVENTS = script.Parent.Parent:WaitForChild("Events")

--[[Camera]]--
local Camera = workspace.CurrentCamera

--[[Character]]--
local char = script.Parent.Parent
local Root = char:WaitForChild("HumanoidRootPart")

local RunService = game:GetService("RunService")

local enemytobefaced

local combatData = char:WaitForChild("combatData")

local StartCameraCFrame = Camera.CFrame

--[[Scripting]]--
local lockedOn = false

-- LERPING

local TargetCameraCFrame
local lerpTime = 0.3
local startTime = tick()
local runningTime = 0
local lerp
local alpha = 0
local enabled = true

local function getLockOnPos(target, targetRoot)

	local startPos = CFrame.lookAt(Root.Position,targetRoot.Position) * CFrame.new(0,10, 13)
	-- this influences how far the camera is set from the avatar (the thing behind is the offset, last number is zoom)

	local endPos = targetRoot.CFrame * CFrame.new(0,0,0)

	-- this y number influences the tilt/pitch, noramlly -4




	return startPos,endPos
end



local function lockOnCamera()
	--Camera.CameraType = Enum.CameraType.Scriptable
	
		startPos, endPos = getLockOnPos(enemytobefaced, enemytobefaced.HumanoidRootPart)

--tween option didn't work because it'd go 'back' to original cframe zoom after playing
	--TWEEN_SERVICE:Create(Camera,TweenInfo.new(0.3,Enum.EasingStyle.Quad),{CFrame = CFrame.lookAt(startPos.Position,endPos.Position)}):Play()
	--wait(0.2)
	--Camera.CFrame = CFrame.lookAt(startPos.Position,endPos.Position) --(setting without tween or lerp)

	
	TargetCameraCFrame = CFrame.lookAt(startPos.Position,endPos.Position)
	
		if enabled then
		-- below: by making lerp the function
			lerp = RunService.Heartbeat:Connect(function(deltaTime)
			runningTime += deltaTime -- how much time has passed since starting this
			alpha = runningTime/lerpTime -- if the lerp time is three seconds and we been running for 1.5
			-- then the alpha would be 0.5 i.e. 50% of the way through, halway through the lerp
			
			Camera.CFrame = StartCameraCFrame:Lerp(TargetCameraCFrame, alpha)
				
			if alpha >= 1 then
				lerp:Disconnect()
				print("complete")
				print(alpha)
				--enabled = false
			end
			
			-- handy thing about lerps is that in the middle of the lerp you can say (with elseif: hey actually go to here)
			end)
		end
	
end




--Bindables

EVENTS.LockOnToggle.Event:Connect(function(target)
	if target then
		lockedOn = true
		enemytobefaced = target
		alpha = 0
		runningTime = 0
		StartCameraCFrame = Camera.CFrame
		--enabled = true
		lockOnCamera()
		--Camera.CameraType = Enum.CameraType.Custom
	--else
	--	lockedOn = false
		
	end
end)




Hello,

I’m not 100% sure if this is particularly what you’re looking for, but you could try adapting and trying this code:

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

local function LockOn(pos)
	Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, pos)
end

EVENTS.LockOnToggle.Event:Connect(function(new_target)
	if lockedOn then
		event:Disconnect()
		lockedOn = false
		target = nil
	else
		lockedOn = true
		target = new_target:FindFirstChild("HumanoidRootPart")
		event = RS.RenderStepped:Connect(function()
			LockOn(target.Position)
		end)
	end
end)

Let me know if it works!