Hello! I am currently working on a game and I am trying to figure out how to use Camera.Focus to determine if the camera is looking at a specific object. If anyone has any help for this, it would be greatly appreciated.
The
Camera.Focus
property represents the point the camera is looking at. It is important this property is set as it also represents where the game thinks you are in the world. Certain visuals will be more detailed and will update more frequently, depending on how close they are to the Focus. Roblox’s default camera scripts take care of this.
Roblox documents mostly everything. Make sure you check if it exists before asking, if you have but it didn’t work / didn’t answer your question, then this probably is fair to post. Here is a link.
The Camera.Focus property is more of a ‘set’ than ‘get’ property.
However, if you wanted to get the object a player is directly looking at or wanted to check if a particular object is being focused in on; I wrote you a script.
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {char}
rayParams.IgnoreWater = true
local part = workspace.Part
local guiInset = game:GetService("GuiService"):GetGuiInset()
local screenCenter = Vector2.new(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2 - (guiInset.Y/2))
RS.RenderStepped:Connect(function()
local vector, onScreen = cam:WorldToScreenPoint(part.Position)
-- if you want to check if a single object is on screen and if it's focused.
if onScreen then
local screenPoint = Vector2.new(vector.X, vector.Y)
local depth = vector.Z
local mag = (screenCenter - screenPoint).Magnitude
if mag <= 200 then
print("Looking Near or At")
end
print("Visible")
end
-- if you just want to see the current focus.
local rayResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*50, rayParams)
if rayResult then
local target = rayResult.Instance
print(target)
end
end)
The first case, figuring out if a particular object is on screen, is simple. Just use WorldToScreenPoint.
However, figuring out if the object is the specific focus is a bit more difficult and requires viewport math. For this I simply calculated the center of the screen using:
local guiInset = game:GetService("GuiService"):GetGuiInset()
local screenCenter = Vector2.new(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2 - (guiInset.Y/2))
With that, we can get the X and Y of the first WorldToScreenPoint result and compare it to the center of the screen. (screenCenter - screenPoint).Magnitude
thus gives us the exact pixel count between the cameras focus and the objects position on screen.
Here I am in first person, with the cursor acting as a marker for the center of the screen.
Moving on, if you simply want to find out the exact object in focus just use a raycast originating from the camera directed towards it’s lookvector.
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {char}
rayParams.IgnoreWater = true
local rayResult = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector*50, rayParams)
if rayResult then
local target = rayResult.Instance
end
Hope this helps!
And if you wanted to grab the script without having to copy-paste it, here’s the library link:
https://www.roblox.com/library/8511705127/CameraFocus