So I’ve been trying to script a lock on target system (a character and a camera looking at the Target) and everything seems to work but there’s only one issue that I can’t solve. Basically whenever I go forward, It goes a bit to the left and the closer I am to the target, the faster I go to the left.
I also realised that if I put 0 at X-axis of CAMERA_OFFSET then it doesn’t happens but I don’t want to have X-axis at 0 but 12. Here’s the script:
local UIS = game:GetService('UserInputService')
local RS = game:GetService('RunService')
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HMRP = Character:WaitForChild('HumanoidRootPart')
local HUM = Character:WaitForChild('Humanoid')
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Target
local MOUSE_HITBOX = Vector3.new(3,3,3)
local CAMERA_OFFSET = Vector3.new(12,2,12)
local CAMERA_LERP = .25
function unLock()
Target = nil
HUM.AutoRotate = true
Camera.CameraType = Enum.CameraType.Custom
script.Target.Value = nil
end
function check(p)
return p:FindFirstChild('HumanoidRootPart') and p:FindFirstChild('Humanoid') and p.Humanoid.Health > 0
end
UIS.InputBegan:Connect(function(k,e)
if(e)then return end
k = k.KeyCode ~= Enum.KeyCode.Unknown and k.KeyCode or k.UserInputType
if(k == Enum.KeyCode.L or k == Enum.UserInputType.MouseButton3)then
if(Target)then
unLock()
return
end
local Hit = workspace:GetPartBoundsInBox(CFrame.new(Mouse.Hit.Position), MOUSE_HITBOX)
for i,v in pairs(Hit) do
local p = v.Parent
if(check(p))then
Target = p
script.Target.Value = p
Camera.CameraType = Enum.CameraType.Scriptable
end
end
end
end)
RS.RenderStepped:Connect(function()
if(Target)then
if not(check(Target)) then
unLock()
return
end
local HMRP2 = Target.HumanoidRootPart.CFrame
local distance = Player:DistanceFromCharacter(Target.HumanoidRootPart.Position)
print(distance)
HUM.AutoRotate = false
HMRP.CFrame = CFrame.lookAt(HMRP.Position, Vector3.new(HMRP2.Position.X, HMRP.Position.Y, HMRP2.Position.Z))
Camera.CFrame = Camera.CFrame:Lerp(CFrame.lookAt((CFrame.lookAt(HMRP.Position, Target.HumanoidRootPart.Position) * CFrame.new(CAMERA_OFFSET)).Position, Target.HumanoidRootPart.Position), CAMERA_LERP)
end
end)