Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make something where if your camera looks at an object, a line of code runs?

I'm making an ORB in a game and I don't know how to make it so if your camera is in it's line of view, it runs a piece of code. This obviously needs to be Local, not for every player. Thanks!

Example:

Player looks at orb

Code runs

Player looks away from the orb

code stops

5 comments

0

Do you have to directly look at the orb or just when it's in view?

0

When it's in view

0

Camera:GetPartsObscuringTarget might help

0

Camera:GetPartsObscuringTarget might help

0

Camera:GetPartsObscuringTarget might help

1 answer

You can try using WorldToScreenPoint function. It checks if a part's position is visible on the screen.

This code snippet utilizes that function to destroy a part.

local camera = workspace.CurrentCamera
local humanoid = script.Parent:WaitForChild('Humanoid')
local part = workspace.ORB
local inView = false

repeat
    local iView = workspace.CurrentCamera:WorldToScreenPoint(part.Position)    
    if (iView) then
        inView = true
    end        
    wait(2)
until inView == true

if inView == true then
    part:Destroy()
end

3 comments

0

Where does the script go? Doesn't it have to be a local script as well? I'm so confused

0

It doesn't work dude.

0

Sorry, put this in a local script in StarterCharacterScripts, obviously replace the part path to wherever you have the part.

This discussion is preserved as it was. New answers, comments, and votes are closed.