Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Parts that know when the player is/isn't looking at them?

Asked by
Troidit 253 Moderation Voter
7 years ago
Edited 7 years ago

I've seen it done on games before so I know it's possible.

I'm trying to create a scenario where when the player looks at an object, looks away, and looks back again, the object is removed/destroyed. I've tried using a local script, and using the function WorldToScreenPoint() with the camera but I failed tragically.

How would I accomplish this?

My failed attempt that gave me the error "Unable to cast array to Vector3"

local cam = workspace.CurrentCamera

while true do
    if cam:WorldToScreenPoint({-28, 0.6, 25}) == true then --checking if anyone is looking at camera
        repeat wait() until cam:WorldToScreenPoint({-28, 0.6, 25}) == false -- waits until they aren't looking at the camera
        game.Workspace.Part:Destory()
    end
end
0
Good Question. User#11440 120 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Using the Camera method is a good idea, do you still have the code with it? If I was in this scenario I would use it. Sorry I cannot use the comments yet, so if you do paste it, ill edit this post again.

EDITED (6:42) - the error you got is because it returns 2 values, a Vector3 and a boolean EDITED (6:44) - Just looked at your code again, the argument for it needs to be a Vector3 also

--[[
It seemed to work for me, but remember it is an example, it is not meant to be good I guess.
]]
local player = game:GetService("Players").LocalPlayer
local workspace = game:GetService("Workspace")
local camera = workspace:FindFirstChild("Camera") --you could do workspace.CurrentCamera
local test = workspace:FindFirstChild("TestPart")
local deb = false 
local counter = 0 

function Looked()
    if (not camera) then return end
    if (not test) then return end
    local a, b = camera:WorldToScreenPoint(test.CFrame.p)

    if (b == true) then 
        deb = true 
    end 

    if (b == false) then 
        if (deb == true) then 
            counter = counter + 1
            print("Looked af") 
        end 
        deb = false 
    end 
end 

game:GetService("RunService").RenderStepped:connect(function()
    Looked()
    if (counter == 2) then --depending on whether you want them to look at it again before it disappear or disappears after one look depends 
        test:Destroy()
    end
end)

my comment did not complete

Theres one thing wrong with it, you should disconnect the event when (RenderStepped) when the part is no more, and instead of upvaluing the test variable from the main scope, you could pass it as an argument in the function

0
Ill make an example TheLowOne 85 — 7y
0
I tried it on a new place that I didn't save. But I can make the code again quickly Troidit 253 — 7y
0
also TheLowOne 85 — 7y
0
The error you got is because it returns 2 results TheLowOne 85 — 7y
Ad

Answer this question