Advertisement
Guest User

Lock On Camera

a guest
Feb 17th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. --< Variables
  2. local Char = script.Parent.Parent
  3. local HPR = Char.HumanoidRootPart
  4. local Children = game.Workspace:GetChildren()
  5. local UIService = game:GetService("UserInputService")
  6. local Targets = nil
  7. local RunService = game:GetService("RunService")
  8. local Target = nil
  9. local TargetNum = nil
  10. local Camera = game.Workspace.CurrentCamera
  11.  
  12. local Circle = script:WaitForChild("Circle")
  13. local Health = game.Players.LocalPlayer.PlayerGui:WaitForChild("Health")
  14.  
  15.  
  16. --< Functions
  17. local getTargetsInOrder = function()
  18. local Chars = {}
  19.  
  20. --Find All Players
  21. for i, v in pairs(Children) do
  22. if v:FindFirstChild("HumanoidRootPart")and
  23. v:FindFirstChild("Humanoid") and
  24. v ~= Char then
  25.  
  26. table.insert(Chars, v)
  27. end
  28. end
  29.  
  30. --Order Characters from Close to Far
  31. local ReturnValue = {Chars[1]}
  32. local ClosestMag =
  33. (ReturnValue[1].HumanoidRootPart.Position -
  34. HPR.Position).magnitude
  35.  
  36. table.remove(Chars, 1)
  37.  
  38. for i, v in pairs(Chars) do
  39. local PlayerPos = HPR.Position
  40. local ThisPos = v.HumanoidRootPart.Position
  41.  
  42. if (PlayerPos - ThisPos).magnitude < ClosestMag then
  43. table.insert(ReturnValue, 1, v)
  44.  
  45. ClosestMag = (PlayerPos - ThisPos).magnitude
  46. else
  47. for a = #ReturnValue, 1, -1 do
  48. if (PlayerPos - ThisPos).magnitude >
  49. (ReturnValue[a].HumanoidRootPart.Position -
  50. PlayerPos).magnitude then
  51.  
  52. table.insert(ReturnValue, a+1, v)
  53. break
  54. end
  55. end
  56. end
  57. end
  58.  
  59. return ReturnValue, #Chars + 1
  60. end
  61.  
  62. --< Event for new Input
  63. UIService.InputBegan:Connect(function(input, engine)
  64. if engine then return end
  65. if not HPR then return end
  66.  
  67. if input.KeyCode == Enum.KeyCode.LeftShift then
  68. Target = getTargetsInOrder()[1]
  69. script.Parent.Target.Value = Target.Name
  70. script.Sound:Play()
  71.  
  72. billboard = Target.Head:WaitForChild("BillboardGui")
  73. billboard.Enabled = true
  74. Health.Enabled = true
  75. TargetNum = 1
  76. Circle.Parent = Target.HumanoidRootPart
  77. Circle.Enabled = true
  78.  
  79. local IsLocked = Instance.new("BoolValue")
  80. IsLocked.Name = "IsLocked"
  81. IsLocked.Parent = Char
  82.  
  83. elseif input.KeyCode == Enum.KeyCode.E and Target then
  84. local _,Chars = getTargetsInOrder()
  85. TargetNum = (TargetNum + 1)
  86. if TargetNum > #getTargetsInOrder() then
  87. TargetNum = 1
  88. end
  89. Target = getTargetsInOrder()[TargetNum]
  90. Circle.Enabled = false
  91. billboard.Enabled = false
  92. print(TargetNum)
  93. Circle.Parent = Target.HumanoidRootPart
  94. Circle.Enabled = true
  95. billboard = Target.Head:FindFirstChild("BillboardGui")
  96. billboard.Enabled = true
  97.  
  98. print(Target)
  99. elseif input.KeyCode == Enum.KeyCode.Q and Target then
  100. local _,Chars = getTargetsInOrder()
  101. TargetNum = (TargetNum - 1)
  102. if TargetNum < 1 then
  103. TargetNum = #getTargetsInOrder()
  104. print("THE NUMBER IS LESS THAN 1 SO ;-;")
  105. end
  106. Circle.Enabled = false
  107. billboard.Enabled = false
  108. print(TargetNum)
  109. Target = getTargetsInOrder()[TargetNum]
  110. Circle.Parent = Target.HumanoidRootPart
  111. Circle.Enabled = true
  112. billboard = Target.Head:FindFirstChild("BillboardGui")
  113. billboard.Enabled = true
  114. end
  115. end)
  116.  
  117. UIService.InputEnded:Connect(function(input)
  118. if input.KeyCode == Enum.KeyCode.LeftShift then
  119. Target = nil
  120. TargetNum = nil
  121. script.Parent.Target.Value = ""
  122.  
  123. billboard.Enabled = false
  124. Circle.Enabled = false
  125. Health.Enabled = false
  126.  
  127. Char:WaitForChild("IsLocked"):destroy()
  128.  
  129.  
  130. end
  131. end)
  132.  
  133. --< RenderStepped Event
  134. local l = 0
  135.  
  136. RunService.RenderStepped:Connect(function()
  137. if Target and Target.Humanoid.Health > 0 then
  138. if (Target.HumanoidRootPart.Position - HPR.Position).magnitude > 15 then
  139. local TargetHRP = Target.HumanoidRootPart
  140. HPR.CFrame = CFrame.new(HPR.Position,TargetHRP.Position)
  141. local offset = Vector3.new(0,4,5)
  142. local camMag = (TargetHRP.Position - Camera.CFrame.p).magnitude
  143.  
  144. local newCFrame = TargetHRP.Position + ((HPR.Position + offset)- TargetHRP.Position).Unit * ((HPR.Position - TargetHRP.Position).Magnitude + 12)
  145. local newFrame = CFrame.new(newCFrame,TargetHRP.Position)
  146.  
  147. Camera.CameraType = Enum.CameraType.Scriptable
  148. Camera.CFrame = Camera.CFrame:Lerp(newFrame, l)
  149. l = l == 1 or l + 0.1 and 0.1
  150. else
  151. local TargetHRP = Target.HumanoidRootPart
  152. HPR.CFrame = CFrame.new(HPR.Position,TargetHRP.Position)
  153.  
  154. Camera.CameraType = Enum.CameraType.Scriptable
  155. Camera.CFrame = Camera.CFrame:Lerp(HPR.CFrame * CFrame.new(-5,5,15), l)
  156. l = l == 1 or l + 0.1 and 0.1
  157. end
  158. else
  159. Camera.CameraType = Enum.CameraType.Custom
  160. end
  161. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement