Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Create a ScreenGui
- local ScreenGui = Instance.new("ScreenGui")
- ScreenGui.Parent = game.CoreGui
- -- Create a rainbow toggle button
- local LockOnButton = Instance.new("TextButton")
- LockOnButton.Parent = ScreenGui
- LockOnButton.Size = UDim2.new(0, 100, 0, 30)
- LockOnButton.Position = UDim2.new(0, 10, 0, 10)
- LockOnButton.BackgroundColor3 = Color3.new(1, 1, 1)
- LockOnButton.Text = "Lock-On"
- LockOnButton.TextScaled = true
- LockOnButton.TextColor3 = Color3.new(0, 0, 0)
- -- Rainbow effect for the button
- spawn(function()
- while wait(0.1) do
- local hue = tick() % 5 / 5
- LockOnButton.BackgroundColor3 = Color3.fromHSV(hue, 1, 1)
- end
- end)
- -- Variables
- local locking = false
- local target = nil
- local userInput = game:GetService("UserInputService")
- local runService = game:GetService("RunService")
- -- Function to find the nearest player to the mouse
- local function getNearestPlayerToMouse()
- local mouse = game.Players.LocalPlayer:GetMouse()
- local nearestDistance = math.huge
- local nearestPlayer = nil
- for _, player in pairs(game.Players:GetPlayers()) do
- if player ~= game.Players.LocalPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
- local screenPoint = workspace.CurrentCamera:WorldToScreenPoint(player.Character.HumanoidRootPart.Position)
- local distance = (Vector2.new(mouse.X, mouse.Y) - Vector2.new(screenPoint.X, screenPoint.Y)).Magnitude
- if distance < nearestDistance then
- nearestDistance = distance
- nearestPlayer = player
- end
- end
- end
- return nearestPlayer
- end
- -- Lock-On functionality with prediction
- local function lockCameraToTarget()
- if locking and target and target.Character and target.Character:FindFirstChild("HumanoidRootPart") then
- local camera = workspace.CurrentCamera
- local targetPart = target.Character.HumanoidRootPart
- local velocity = targetPart.Velocity
- local predictionTime = 0.1 -- Time in seconds to predict ahead
- -- Predict the target's future position based on velocity
- local predictedPosition = targetPart.Position + (velocity * predictionTime)
- -- Keep the camera at its normal position but make it look at the predicted position
- camera.CameraType = Enum.CameraType.Custom
- camera.CFrame = CFrame.new(camera.CFrame.Position, predictedPosition)
- else
- -- Reset the camera to default when not locking on
- locking = false
- target = nil
- end
- end
- -- Toggle Lock-On when pressing 'E'
- userInput.InputBegan:Connect(function(input, gameProcessed)
- if not gameProcessed and input.KeyCode == Enum.KeyCode.E then
- locking = not locking
- if locking then
- target = getNearestPlayerToMouse()
- if target then
- runService.RenderStepped:Connect(lockCameraToTarget)
- else
- locking = false
- end
- else
- -- Turn off lock-on
- target = nil
- end
- end
- end)
- -- Stop lock-on if the player or target dies
- game.Players.LocalPlayer.CharacterAdded:Connect(function()
- locking = false
- target = nil
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- if player == target then
- locking = false
- target = nil
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement