Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local LocalPlayer = Players.LocalPlayer
- local Camera = workspace.CurrentCamera
- local UserInputService = game:GetService("UserInputService")
- local RunService = game:GetService("RunService")
- local aimEnabled = false
- local lockedPlayer = nil -- currently locked target
- -- Create and configure the FOV drawing circle
- local fovCircle = Drawing.new("Circle")
- fovCircle.Color = Color3.fromRGB(255, 255, 255)
- fovCircle.Transparency = 0.7
- fovCircle.NumSides = 100
- fovCircle.Radius = 100
- fovCircle.Position = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
- fovCircle.Visible = true
- -- Check if a player's character is within the FOV circle
- local function isPlayerInFOV(player)
- local character = player.Character
- if character and character:FindFirstChild("HumanoidRootPart") then
- local screenPosition, onScreen = Camera:WorldToScreenPoint(character.HumanoidRootPart.Position)
- if onScreen then
- local distance = (Vector2.new(screenPosition.X, screenPosition.Y) - fovCircle.Position).Magnitude
- return distance <= fovCircle.Radius
- end
- end
- return false
- end
- -- Aim camera at the target player's HumanoidRootPart
- local function aimAtPlayer(player)
- local character = player.Character
- if character and character:FindFirstChild("HumanoidRootPart") then
- local targetPosition = character.HumanoidRootPart.Position
- local cameraCFrame = CFrame.new(Camera.CFrame.Position, targetPosition)
- Camera.CFrame = cameraCFrame
- end
- end
- -- Teleport the local player's character behind the target player
- local function teleportBehindPlayer(targetPlayer)
- local targetCharacter = targetPlayer.Character
- local localCharacter = LocalPlayer.Character
- if targetCharacter and targetCharacter:FindFirstChild("HumanoidRootPart") and
- localCharacter and localCharacter:FindFirstChild("HumanoidRootPart") then
- local targetHRP = targetCharacter.HumanoidRootPart
- local offsetDistance = 5 -- distance behind the target
- -- Calculate behind position: subtract the target's LookVector scaled by offsetDistance
- local behindPosition = targetHRP.Position - (targetHRP.CFrame.LookVector * offsetDistance)
- -- Set local player's character CFrame so that it faces the target
- local newCFrame = CFrame.new(behindPosition, targetHRP.Position)
- localCharacter:MoveTo(behindPosition)
- -- Alternatively, you can set the CFrame directly:
- -- localCharacter.HumanoidRootPart.CFrame = newCFrame
- end
- end
- -- Toggle aim and clear locked target when turning off
- UserInputService.InputBegan:Connect(function(input, gameProcessed)
- if not gameProcessed and input.KeyCode == Enum.KeyCode.T then
- aimEnabled = not aimEnabled
- if not aimEnabled then
- lockedPlayer = nil
- end
- end
- end)
- -- Main loop to update aim and teleportation
- RunService.RenderStepped:Connect(function()
- if aimEnabled then
- -- If no locked target, look for one within the FOV
- if not lockedPlayer or not (lockedPlayer.Character and lockedPlayer.Character:FindFirstChild("HumanoidRootPart")) then
- lockedPlayer = nil -- reset if invalid
- for _, player in ipairs(Players:GetPlayers()) do
- if player ~= LocalPlayer and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
- if isPlayerInFOV(player) then
- lockedPlayer = player
- break
- end
- end
- end
- end
- -- If a target is locked, aim and teleport behind them continuously
- if lockedPlayer then
- aimAtPlayer(lockedPlayer)
- teleportBehindPlayer(lockedPlayer)
- end
- end
- end)
- -- Update the FOV circle's position on the screen each frame
- RunService.RenderStepped:Connect(function()
- fovCircle.Position = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
- end)
Advertisement
Add Comment
Please, Sign In to add comment