Lock-On camera smoothly transition from one enemy to another

So I am making a “Lock-On” Camera for my sword combat system. When the player press Ctrl on there keyboard, the camera automatically point and lock toward the nearest enemy. Which is working fine right now.

The problem is that when the camera start to switch between the last nearest enemy to the new one, the camera will just cut and immediately points toward the new enemy . I want the camera to smoothly transition and not just cut and immediately points toward the new enemy. So if you have any idea on how to do that please help me out. Thanks in advance!

Here’s my lock-on script

UIS.InputBegan:Connect(function(key, typing)
	if typing then return end
	if key.KeyCode == Enum.KeyCode.LeftControl then
		if isLock == false then
			isLock = true
		else
			isLock = false
			workspace.CurrentCamera.CameraType = "Custom"
		end
	end
end)

RS.RenderStepped:Connect(function()
	if isLock == true then
		local nearestDistance
		for _, enemy in pairs(game.Workspace.Enemy:GetChildren()) do
			local distance = (char.HumanoidRootPart.Position - enemy.PrimaryPart.Position).Magnitude
			if not enemy or 
				distance > maxDistance or
				(nearestDistance and distance >= nearestDistance)
			then
				continue
			end
			nearestDistance = distance
			target = enemy
		end

		if target and (char.HumanoidRootPart.Position - target.PrimaryPart.Position).Magnitude < maxDistance and target.Humanoid.Health > 0 then
			local Campos = CFrame.new(char.HumanoidRootPart.Position, target.PrimaryPart.Position) * CFrame.new(0,6,9).p
			workspace.CurrentCamera.CameraType = "Scriptable"
			workspace.CurrentCamera.CFrame = CFrame.new(Campos , target.PrimaryPart.Position)
		else
			workspace.CurrentCamera.CameraType = "Custom"
			target = nil
		end
	end
end)
5 Likes


Btw here’s the video of the problem (It won’t let me upload it on the post)

Use :Lerp()

In your case:

workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame:Lerp(targetCFrame, 0.1)

Change the number between 0 and 1 to find which speed of transition is best.

One of the problems with this method is that it also gives the effect of the camera trying to “catch up” to the player.

1 Like

Use tween service for the camera cframes instead of modifying it directly

As well as making it smooth between targets it gives a nicer smoothness effect with movement in general on the camera

game:GetService("TweenService"):Create(workspace.CurrentCamera,TweenInfo.new(.1,Enum.EasingStyle.Quad),{CFrame = goalCFrame})
1 Like