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)