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

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

Asked by 2 years ago

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

0
Do you have to directly look at the orb or just when it's in view? DocGooseYT 110 — 2y
0
When it's in view Krektonix_Youtube 85 — 2y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 2y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 2y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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

0
Where does the script go? Doesn't it have to be a local script as well? I'm so confused Krektonix_Youtube 85 — 2y
0
It doesn't work dude. Krektonix_Youtube 85 — 2y
0
Sorry, put this in a local script in StarterCharacterScripts, obviously replace the part path to wherever you have the part. MendozaHM3SBulldog 32 — 2y
Ad

Answer this question