Howdy, I’m working on creating a game (more or less a passion project) and part of that game is going to be an overhead drone system that can spot and highlight players that are visible on screen. And un-highlight them if they’re obstructed / off screen. This part is done. Keep in mind that the code below is no where near the final product. I’m purely running things from a testing perspective
- What is the issue? Include screenshots / videos if possible
The issue is that there appear to be “Dead Zones” around parts where even thought the character is clearly visible, the game does not recognize them to be, thus, the character is not being highlighted within these zones.
[Video]
robloxapp-20230531-1137301.wmv (1.4 MB)
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I used answers from places like how to make a part move within a radius of another part and how to check if an object is visible or not to get to the code I have currently. I’ve also tried returning the name of the part it was saying that was obscuring the player, but it was returning nil every time.
Here is the full code below, and the layout you’ll need in roblox studio to test for yourself.
local plr = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local toggle = false
local cam = game.Workspace.CurrentCamera
local offset = Vector3.new(60,80,60)
local rotSpeed = 0.01
function circleTarget(part,degree)
local newCFrame = part.CFrame + Vector3.new(offset.Y * math.cos(degree),offset.Y,offset.Y * math.sin(degree))
degree +=rotSpeed
local newpos = newCFrame.Position
return newpos,degree
end
local function highlightVisiblePlayers(t)
for _,v in game.Players:GetChildren() do
if v.Character:FindFirstChildWhichIsA("Humanoid") then
local _, OnScreen = workspace.CurrentCamera:WorldToScreenPoint(t.Position)
if OnScreen then
--print(v.Name .. " Is on Screen")
local partsObscuring = workspace.CurrentCamera:GetPartsObscuringTarget({workspace.CurrentCamera.CFrame.Position,v.Character.HumanoidRootPart.Position},{v})
if #partsObscuring == 0 then
print(v.Name.." Is not obscured")
if not v.Character:FindFirstChildWhichIsA("Highlight") then
local H = Instance.new("Highlight")
H.FillColor = Color3.new(0,0,0)
H.Parent = v.Character
else
print(v.Name.." Is OnScreen, Obscured")
local Highlight = v.Character:FindFirstChildWhichIsA("Highlight")
if Highlight then
Highlight:Destroy()
end
end
else
print(v.Name.." Is OffScreen.")
local Highlight = v.Character:FindFirstChildWhichIsA("Highlight")
if Highlight then
Highlight:Destroy()
end
end
end
end
end
end
function ToggleCamView()
warn("ToggleCamView Called.")
toggle = not toggle
if toggle then
local rot = 0
print("entering cam")
cam.CameraType = Enum.CameraType.Scriptable
local target = game.Workspace.SpawnLocation
local tpos = target.Position
local newpos = tpos + offset
cam.CFrame = CFrame.lookAt(newpos,tpos)
warn("initial cframe set.")
plr.PlayerGui["DroneUI"].Enabled = true
print("Entering while loop.")
while toggle do
highlightVisiblePlayers(target)
newpos,rot = circleTarget(target,rot)
cam.CFrame = CFrame.lookAt(newpos,tpos)
RS.RenderStepped:Wait()
end
else
print("exiting cam")
plr.PlayerGui["DroneUI"].Enabled = false
cam.CameraType = Enum.CameraType.Custom
for _,v in game.Workspace:GetDescendants() do
if v:IsA("Highlight") then
v:Destroy()
end
end
end
end
local function onInputBegan(input, _gameProcessed)
if _gameProcessed then
print("Player is typing")
elseif input.KeyCode == Enum.KeyCode.X then
print("The X button has been pressed!")
ToggleCamView()
end
end
UserInputService.InputBegan:Connect(onInputBegan)
My primary concern is the dead zoning issue. The map that I’ve built and plan to put this system into has a lot of buildings, all of which have interiors. So being really good at telling when a player is on screen and unobstructed or not is crucial. Thanks guys! I have a strong hunch what is causing the issue is the parts that make up the player getting in the way of the HRP. I’ve tried switching the part to the head instead of the HRP, but the script wouldn’t highlight at all at that point.
For setup and testing, please mimic this layout: