Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Toggle Lock-on and Enemy Player Movement Prediction Script for Roblox
- local UserInputService = game:GetService("UserInputService")
- local Players = game:GetService("Players")
- local player = Players.LocalPlayer
- local camera = game.Workspace.CurrentCamera
- local lockedEnemy = nil
- local isLockedOn = false
- local isPredictingMovement = false
- -- Function to find the nearest enemy player
- local function findNearestEnemy()
- local nearestEnemy = nil
- local minDistance = math.huge
- local playerPosition = camera.CFrame.Position
- for _, otherPlayer in pairs(Players:GetPlayers()) do
- if otherPlayer ~= player and otherPlayer.TeamColor ~= player.TeamColor and otherPlayer.Character and otherPlayer.Character:FindFirstChild("HumanoidRootPart") then
- local distance = (otherPlayer.Character.HumanoidRootPart.Position - playerPosition).Magnitude
- if distance < minDistance then
- minDistance = distance
- nearestEnemy = otherPlayer
- end
- end
- end
- return nearestEnemy
- end
- -- Function to update camera position towards target enemy 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 enemy player movement
- local function predictEnemyMovement(target)
- if target and target.Character and target.Character:FindFirstChild("Humanoid") then
- local humanoid = target.Character.Humanoid
- local currentPosition = target.Character.HumanoidRootPart.Position
- local currentVelocity = humanoid.RootPart.Velocity
- -- Predict future position based on current position and velocity
- local predictionTime = 1.0 -- Adjust prediction time as needed
- local predictedPosition = currentPosition + currentVelocity * predictionTime
- camera.CFrame = CFrame.new(camera.CFrame.p, predictedPosition)
- end
- end
- -- Function to handle key press
- local function onKeyPress(input)
- if input.KeyCode == Enum.KeyCode.Equals then
- isLockedOn = not isLockedOn
- if isLockedOn then
- lockedEnemy = findNearestEnemy()
- end
- elseif input.KeyCode == Enum.KeyCode.Z then
- isPredictingMovement = not isPredictingMovement
- end
- end
- -- Connect the key press function to the UserInputService
- UserInputService.InputBegan:Connect(onKeyPress)
- -- Main loop to update camera position
- game:GetService("RunService").RenderStepped:Connect(function()
- if isLockedOn and lockedEnemy and lockedEnemy.Character and lockedEnemy.Character:FindFirstChild("HumanoidRootPart") then
- if isPredictingMovement then
- predictEnemyMovement(lockedEnemy)
- else
- updateCameraPosition(lockedEnemy)
- end
- end
- end)
- -- Check if the locked enemy player or the player who executed the script dies
- player.Character.Humanoid.Died:Connect(function()
- lockedEnemy = nil
- isLockedOn = false
- end)
- Players.PlayerRemoving:Connect(function(removedPlayer)
- if lockedEnemy == removedPlayer then
- lockedEnemy = nil
- isLockedOn = false
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement