Advertisement
skibidisigma2555

lock on with prediction ( e to toggle btw

Jan 28th, 2025
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. -- Create a ScreenGui
  2. local ScreenGui = Instance.new("ScreenGui")
  3. ScreenGui.Parent = game.CoreGui
  4.  
  5. -- Create a rainbow toggle button
  6. local LockOnButton = Instance.new("TextButton")
  7. LockOnButton.Parent = ScreenGui
  8. LockOnButton.Size = UDim2.new(0, 100, 0, 30)
  9. LockOnButton.Position = UDim2.new(0, 10, 0, 10)
  10. LockOnButton.BackgroundColor3 = Color3.new(1, 1, 1)
  11. LockOnButton.Text = "Lock-On"
  12. LockOnButton.TextScaled = true
  13. LockOnButton.TextColor3 = Color3.new(0, 0, 0)
  14.  
  15. -- Rainbow effect for the button
  16. spawn(function()
  17. while wait(0.1) do
  18. local hue = tick() % 5 / 5
  19. LockOnButton.BackgroundColor3 = Color3.fromHSV(hue, 1, 1)
  20. end
  21. end)
  22.  
  23. -- Variables
  24. local locking = false
  25. local target = nil
  26. local userInput = game:GetService("UserInputService")
  27. local runService = game:GetService("RunService")
  28.  
  29. -- Function to find the nearest player to the mouse
  30. local function getNearestPlayerToMouse()
  31. local mouse = game.Players.LocalPlayer:GetMouse()
  32. local nearestDistance = math.huge
  33. local nearestPlayer = nil
  34.  
  35. for _, player in pairs(game.Players:GetPlayers()) do
  36. if player ~= game.Players.LocalPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
  37. local screenPoint = workspace.CurrentCamera:WorldToScreenPoint(player.Character.HumanoidRootPart.Position)
  38. local distance = (Vector2.new(mouse.X, mouse.Y) - Vector2.new(screenPoint.X, screenPoint.Y)).Magnitude
  39. if distance < nearestDistance then
  40. nearestDistance = distance
  41. nearestPlayer = player
  42. end
  43. end
  44. end
  45.  
  46. return nearestPlayer
  47. end
  48.  
  49. -- Lock-On functionality with prediction
  50. local function lockCameraToTarget()
  51. if locking and target and target.Character and target.Character:FindFirstChild("HumanoidRootPart") then
  52. local camera = workspace.CurrentCamera
  53. local targetPart = target.Character.HumanoidRootPart
  54. local velocity = targetPart.Velocity
  55. local predictionTime = 0.1 -- Time in seconds to predict ahead
  56.  
  57. -- Predict the target's future position based on velocity
  58. local predictedPosition = targetPart.Position + (velocity * predictionTime)
  59.  
  60. -- Keep the camera at its normal position but make it look at the predicted position
  61. camera.CameraType = Enum.CameraType.Custom
  62. camera.CFrame = CFrame.new(camera.CFrame.Position, predictedPosition)
  63. else
  64. -- Reset the camera to default when not locking on
  65. locking = false
  66. target = nil
  67. end
  68. end
  69.  
  70. -- Toggle Lock-On when pressing 'E'
  71. userInput.InputBegan:Connect(function(input, gameProcessed)
  72. if not gameProcessed and input.KeyCode == Enum.KeyCode.E then
  73. locking = not locking
  74. if locking then
  75. target = getNearestPlayerToMouse()
  76. if target then
  77. runService.RenderStepped:Connect(lockCameraToTarget)
  78. else
  79. locking = false
  80. end
  81. else
  82. -- Turn off lock-on
  83. target = nil
  84. end
  85. end
  86. end)
  87.  
  88. -- Stop lock-on if the player or target dies
  89. game.Players.LocalPlayer.CharacterAdded:Connect(function()
  90. locking = false
  91. target = nil
  92. end)
  93.  
  94. game.Players.PlayerRemoving:Connect(function(player)
  95. if player == target then
  96. locking = false
  97. target = nil
  98. end
  99. end)
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement