Hello!, How could I make it so that when you see a Model/Block, the console prints a text?, Basically like in the slenderman games, I couldn’t find a method for this
Calling workspace.CurrentCamera:WorldToViewportPoint(thingToSee.CFrame.Position) will return two variables, the second of which is whether or not thingToSee’s position is on-screen or not.
Check out the documentation here: Camera:WorldToViewportPoint
adding onto what @MP3Face said, heres a script that does that
--- local script inside starter player scripts
local Camera = workspace.CurrentCamera
local Object:Model = workspace.BluePart -- change to model/part
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
if Object:IsA("Model") then
for i,v in pairs(Object:GetDescendants()) do
local _, Cansee = Camera:WorldToViewportPoint(v)
if Cansee == true then -- if the player sees it
print('player can see object')
end
end
elseif Object:IsA("BasePart") then
local _, Cansee = Camera:WorldToViewportPoint(Object.CFrame.Position)
if Cansee == true then -- if the player sees it
print('player can see object')
end
end
end)
First of all “== true” is redundant (because if the variable “Cansee” is not a reference to nil/false then it is truthy by default).
The first part of that connected callback function won’t work as you’re iterating over the descendants of a “Model” instance and passing each instance as an argument to the instance method “:WorldToViewportPoint()” which expects a position in the form/type of a Vector3 value of the Vector3 class. Finally the model’s variable declaration is incorrect.
local Object:Model = workspace.BluePart --colon isnt necessary here
local Object = workspace.BluePart --correct declaration
Anyway, I’ve decided to write my own implementation for this task too.
local Camera = workspace.CurrentCamera
local Model = workspace:WaitForChild("Model")
local RunService = game:GetService("RunService")
local function FindPartsRecursive(Object)
if Object:IsA("Model") then
for _, Child in ipairs(Object:GetChildren()) do
FindPartsRecursive(Child)
end
elseif Object:IsA("BasePart") then
local _, InViewport = Camera:WorldToViewportPoint(Object.Position)
if InViewport then
print(Model.Name.." is currently in the viewport.")
end
end
end
RunService.RenderStepped:Connect(function()
FindPartsRecursive(Model)
end)