Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Toggle Lock-on and Player Movement Prediction Script for Roblox
- local UserInputService = game:GetService("UserInputService")
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local player = Players.LocalPlayer
- local camera = game.Workspace.CurrentCamera
- local lockedPlayer = nil
- local isLockedOn = false
- local isPredictingMovement = false
- local isSpectating = false
- -- Function to find the nearest player
- local function findNearestPlayer()
- local nearestPlayer = nil
- local minDistance = math.huge
- local playerPosition = camera.CFrame.Position
- for _, otherPlayer in pairs(Players:GetPlayers()) do
- if otherPlayer ~= player and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
- local distance = (otherPlayer.Character.HumanoidRootPart.Position - playerPosition).Magnitude
- if distance < minDistance then
- minDistance = distance
- nearestPlayer = otherPlayer
- end
- end
- end
- return nearestPlayer
- end
- -- Function to update camera position towards target player
- local function updateCameraPosition(target)
- if target and target.Character and target.Character:FindFirstChild("HumanoidRootPart") then
- local targetPosition = target.Character.HumanoidRootPart.Position
- camera.CFrame = CFrame.new(camera.CFrame.p, targetPosition)
- end
- end
- -- Function to predict player movement
- local function predictPlayerMovement(target)
- if target and target.Character and target.Character:FindFirstChild("HumanoidRootPart") then
- local rootPart = target.Character.HumanoidRootPart
- local currentPosition = rootPart.Position
- local currentVelocity = rootPart.Velocity
- -- Improved prediction logic
- local distance = (currentPosition - camera.CFrame.Position).Magnitude
- local predictionTime = math.clamp(distance / 50, 0.1, 2) -- Adjust the divisor for tuning
- local predictedPosition = currentPosition + currentVelocity * predictionTime
- -- Smooth prediction by averaging current and predicted positions
- local smoothFactor = 0.8 -- Adjust for smoother transitions
- local finalPosition = currentPosition:Lerp(predictedPosition, smoothFactor)
- camera.CFrame = CFrame.new(camera.CFrame.p, finalPosition)
- end
- end
- -- Function to spectate the locked player
- local function spectatePlayer(target)
- if target and target.Character and target.Character:FindFirstChild("HumanoidRootPart") then
- camera.CameraSubject = target.Character.HumanoidRootPart
- end
- end
- -- Function to handle key press
- local function onKeyPress(input)
- if input.KeyCode == Enum.KeyCode.Equals then
- isLockedOn = not isLockedOn
- if isLockedOn then
- lockedPlayer = findNearestPlayer()
- end
- elseif input.KeyCode == Enum.KeyCode.Z then
- isPredictingMovement = not isPredictingMovement
- elseif input.KeyCode == Enum.KeyCode.P then
- if isLockedOn and lockedPlayer then
- isSpectating = not isSpectating
- if not isSpectating then
- camera.CameraSubject = player.Character.HumanoidRootPart
- end
- end
- end
- end
- -- Connect the key press function to the UserInputService
- UserInputService.InputBegan:Connect(onKeyPress)
- -- Main loop to update camera position
- RunService.RenderStepped:Connect(function()
- if isLockedOn and lockedPlayer and lockedPlayer.Character and lockedPlayer.Character:FindFirstChild("HumanoidRootPart") then
- if isSpectating then
- spectatePlayer(lockedPlayer)
- if isPredictingMovement then
- predictPlayerMovement(lockedPlayer)
- end
- elseif isPredictingMovement then
- predictPlayerMovement(lockedPlayer)
- else
- updateCameraPosition(lockedPlayer)
- end
- end
- end)
- -- Check if the locked player or the player who executed the script dies
- player.Character.Humanoid.Died:Connect(function()
- lockedPlayer = nil
- isLockedOn = false
- isSpectating = false
- camera.CameraSubject = player.Character.HumanoidRootPart
- end)
- Players.PlayerRemoving:Connect(function(removedPlayer)
- if lockedPlayer == removedPlayer then
- lockedPlayer = nil
- isLockedOn = false
- isSpectating = false
- camera.CameraSubject = player.Character.HumanoidRootPart
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement