Target lock on help

So I have my custom OTS Camera and I’m trying to make a target lock on system which works really well except one problem. When Unlock from the target the camera returns to the previous position of where the player was looking at and can be quite disruptive and non smooth during gameplay. Here is an example of what I mean:

https://gyazo.com/023f7757273e1156fc7cfa52f3526b08

Here is the block of code for updating the camera:

--[[ Update Camera ]]--
			local StartCFrame = CFrame.new((HumanoidRootPart.CFrame.p + Vector3.new(0, 2, 0))) * CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0)
			local Offset = Camera.CameraSettings.Offset
			local CameraCFrame = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, Offset.Z))
			local CameraFocus = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, -50000))
			local NewCameraCFrame = CFrame.new(CameraCFrame.p, CameraFocus.p) * (ShakeCFrame.Value)
			
			if (CombatProperties.Target.Value ~= nil) then
				
				Offset = Vector3.new(Offset.X + 2, 2, Offset.Z)
				CameraCFrame = CFrame.new(Character.HumanoidRootPart.Position, CombatProperties.Target.Value.PrimaryPart.Position) * Offset
				NewCameraCFrame = CurrentCamera.CFrame:Lerp(CFrame.new(CameraCFrame, CombatProperties.Target.Value.PrimaryPart.Position) * (ShakeCFrame.Value), 0.5)
				
			end

When the player unlocks from the target I want the camera to stay where it is positioned but the player can just freely move their camera again.

Anyone have an idea I’m still stuck on this and not sure what to do ?

Here is the code for camera movement:

ContextActionService:BindAction("CameraMovement", function(ActionName, InputState, InputObject)
	if (CombatProperties.Target.Value ~= nil) then return end
	if (InputObject.UserInputType ~= Enum.UserInputType.Gamepad1) then

		xAngle = xAngle - InputObject.Delta.x * 0.4
		yAngle = math.clamp(yAngle - InputObject.Delta.y * 0.4, -75, 75)

	end
end, false, Enum.UserInputType.MouseMovement)

is your code structured so that:

  • every frame you set the camera cframe (if a target is selected) and thus overwrite the default camerascript
  • if no target is selected then the camera stops being overwritten by your script

because if so that could mean that you need to somehow set the default camerascript’s last cameracframe to your own code’s last cameracframe

The camera doesn’t stop being updated when no target is selected as the script also updates the CFrame for an Over the shoulder view so the CameraType is always set to Scriptable