my mobile lock on camera script has been bugging out which i have come to fix ever so slightly, the camera breaks when trying to lock onto the player and model/other player. there arent any bugs in the output or anything just broken camera which fixes itself when you move but cuts camera to black and kills you when trying to relock on
A video:
Heres the script:
local camera = game.Workspace.Camera
local filter = {}
local RS = game:GetService("RunService")
local Target = nil
Enabled = true
local CamBlock = Instance.new("Part", workspace.CurrentCamera)
CamBlock.Size = Vector3.new(1,1,1)
CamBlock.Transparency = 1
CamBlock.CanCollide = false
local BP = Instance.new("BodyPosition", CamBlock)
BP.maxForce = Vector3.new(math.huge,math.huge,math.huge)
BP.D = 500
local CAS = game:GetService("ContextActionService")
Player = game.Players.LocalPlayer
c = Player.Character
function CancelTarget()
if Enabled == true then
Target = nil
end
end
local CT = CAS:BindAction("CancelTarget", CancelTarget, true, "M")
CAS:SetPosition("CancelTarget", UDim2.new(0.72, -105, 0.10, -100))
uis.TouchTapInWorld:Connect(function(position, processed)
local ray = camera:ViewportPointToRay(position.X, position.Y, 0)
ray = Ray.new(ray.Origin, ray.Direction * 500)
local target, point, surface = workspace:FindPartOnRayWithIgnoreList(ray, filter)
table.insert(filter, Player.Character)
if target == nil then return end
if target.Parent:findFirstChild("Humanoid") and Target == nil then
Target = target.Parent
CamBlock.Position = CFrame.new(c.HumanoidRootPart.Position, Target.PrimaryPart.Position)*CFrame.new(3,3,9).p
BP.Position = CFrame.new(c.HumanoidRootPart.Position, Target.PrimaryPart.Position)*CFrame.new(3,3,9).p
else
end
spawn(function()
print(target)
if target == nil then
print("Nil Ray")
Target = nil
workspace.CurrentCamera.CameraType = "Custom"
CamBlock.Position = nil
end
end)
RS.RenderStepped:connect(function()
if target == nil then return end
if Target and (c.HumanoidRootPart.Position-Target.PrimaryPart.Position).magnitude < 60 and Target.Humanoid ~= nil and Target.Humanoid.Health > 0 and c.Humanoid ~= nil and c.Humanoid.Health > 0 then
local CamPos = CFrame.new(c.HumanoidRootPart.Position, Target.PrimaryPart.Position)*CFrame.new(3,3,9).p
c.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(c.HumanoidRootPart.Position.X,c.HumanoidRootPart.Position.Y,c.HumanoidRootPart.Position.Z),Vector3.new(Target.HumanoidRootPart.Position.X,c.HumanoidRootPart.Position.Y,Target.HumanoidRootPart.Position.Z))
BP.Position = CamPos
workspace.CurrentCamera.CameraType = "Scriptable"
workspace.CurrentCamera.CFrame = CFrame.new(CamBlock.Position,Target.PrimaryPart.Position)
else
Target = nil
workspace.CurrentCamera.CameraType = "Custom"
end
end)
end)``` please any response to fix this would be heavily appreciated