Hello! So basically, I currently have a simple camera system, which just sets the player’s CameraObject to a certain part (Attach CameraType it is I think).
Tho, I’d like to make it so that when a player is in the view of the camera-using player in a certain distance, it will basically make a “scan” effect on them. It would put a rectangle around their character, state their name etcetera.
How could I achieve this? Don’t get me wrong, I’m not asking for full scripts with no work, I’m just not too sure how to achieve this.
Without any exact code, here’s how I think you could achieve this:
To check the camera’s line of sight, you could do a raycast from the camera to each player and see if there are any objects in the way: if there aren’t, then scan the player. (this would have the problem of the player being halfway visible and still have objects in the way: you can decide how to handle this, wether it is to do multiple raycasts for different body parts or keep it as is)
For the “rectangle” effect, you could simply create a new highlight instance and set its adornee to the scanned player model. If you want a more blocky look, you can use a selection box instance.
Last of all, to display the info, you could either create a GUI that displays it on the side or create a BillboardGUI, set its adornee to any of the target’s parts (maybe the HumanoidRootPart), and put the info there.
Of course, you should do these operations (or, at least, 2 and 3) locally for the player on cams so other player’s don’t see themselves being highlighted.
Thank you very much. Though how exactly would I do it so that it shoots Raycasts only to players that are currently in the sight of the player? (Aka can be seen by the Camera lookVector).
Because if I shot Raycasts to all the players (if their Magnitude distance was close enough), it would shoot even to players that the security-camera-operating player doesn’t currently see, no?
You can use :ToObjectSpace() to transform the Character’s CFrame relative to the Camera’s and check if the Z position is positive.
In code, this would be something like this:
local Character -- Player's character
local CameraCFrame -- The CFrame of the camera
if CameraCFrame:ToObjectSpace(Character.PrimaryPart.CFrame).Position.Z > 0 then
-- Player is in front, raycast
else
-- Player is not in front, leave empty or do whatever you want
end
Of course, this only solves the problem for players behind the camera… To be more precise, you should do something like check the angle between the camera’s LookVector and the Vector representing the character’s position relative to the camera: if it’s smaller than the FOV, then raycast. I can’t think on how to do that here on the spot, but by looking a bit you should find something.