So im scripting a camera system that tracks a player if i player is in view of a camera but it doesnt track all the time
local GetThingsInView = function(Delta)
if Character:FindFirstChild("Camera") then
local Camera_Ray = workspace:Raycast(workspace.CurrentCamera .CFrame.Position, Camera.CFrame.LookVector * 150)
local Camera_Tables = Get_Camera()
if Camera_Ray then
if Camera_Ray.Instance.Parent.Parent:FindFirstChild("Humanoid") then
local Target = Camera_Ray.Instance.Parent.Parent
local Distance = (Camera.CFrame.Position - Target.PrimaryPart.Position).Magnitude
if Distance <= 50 then
print("Visible")
Framework_Gui.Full_Frame.CamFrame.Tracker.Text = "CURRENTLY TRACKING : ".."<font color=\"#affed0\">"..string.upper(Target.Name).."</font>"
Framework_Gui.Full_Frame.CamFrame.Distance.Text = "DISTANCE : ".."<font color=\"#affed0\">"..Distance.."</font>"
Camera.CFrame = CFrame.lookAt(Camera.CFrame.Position, Target.PrimaryPart.Position)
else
print("Not")
end
else
if Camera_Tables then
Camera.CFrame = Camera_Tables.Position
Framework_Gui.Full_Frame.CamFrame.Tracker.Text = "CURRENTLY TRACKING : ".."<font color=\"#fe4141\">".."NO ONE".."</font>"
Framework_Gui.Full_Frame.CamFrame.Distance.Text = "DISTANCE : ".."<font color=\"#fe4141\">".."NIL".."</font>"
end
end
end
end
end
This only checks if the player is directly in the center of the viewport. I would suggest taking a look into Camera | Roblox Creator Documentation and see if that helps. The only downside to this is that you can realistically only detect players.
As @NWhut has described, you’re only checking to see if the camera is directly pointing at a player. A better method of determining if a player is directly visible is to raycast from the camera’s origin to a body part of the player, like the head.
To get a ray from the camera to a player’s head, you can construct a ray with the camera’s origin and the player’s head’s position subtracted from the camera origin to get the direction. You can use this ray to get a proper raycast to the player.
Example code:
local function IsPlayerVisibleFromCamera(camera, player)
-- Always remember to check if the objects that you need even exist.
-- The player's character might not be loaded in yet, or they've died and a
-- new character is still in the process of loading in.
-- You should be able to write that yourself. :P
local origin = camera.Position
-- The ray will be the exact length needed, no need to get the unit
local direction = player.Character.Head.Position - camera.Position
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
-- Don't want these pesky things getting in the way...
-- If your camera is in a model, I recommend perhaps filtering out its model
-- instead of just the `camera` part.
params.FilterDescendentsInstances = { camera, player.Character }
-- If there is no result, everything's clear.
return workspace:Raycast(origin, direction, params) == nil
end
you can use Camera | Roblox Creator Documentation but the problem is that it wont detect the player via raycast so that mean if there is a wall behind the player it will just say that the player is visible so in order to fix that you can just check if the player is in the camera then you can send a raycast to check if there is any wall like this example below (try using it with a loop):
function GetPlayerWihtRaycastAndWorldPoint(plr)
local vector, OnScreen = camera:WorldToScreenPoint(plr.Character.HumanoidRootPart)
if OnScreen then --if the player is on screen the start raycasting to see any wall behind the player
local ResultRay = workspace:Raycast(workspace.CurrentCamera.CFrame.Position,
plr.Character.HumanoidRootPart)
if ResultRay then
if ResultRay.Name == plr.Character.Name then
---do stuff if the stuff it raycasted is the player character name or the character itself
end
end
end
end