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 3 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 — 3y
0
When it's in view Krektonix_Youtube 85 — 3y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 3y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 3y
0
Camera:GetPartsObscuringTarget might help SuperPuiu 497 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 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.

01local camera = workspace.CurrentCamera
02local humanoid = script.Parent:WaitForChild('Humanoid')
03local part = workspace.ORB
04local inView = false
05 
06repeat
07    local iView = workspace.CurrentCamera:WorldToScreenPoint(part.Position)   
08    if (iView) then
09        inView = true
10    end       
11    wait(2)
12until inView == true
13 
14if inView == true then
15    part:Destroy()
16end
0
Where does the script go? Doesn't it have to be a local script as well? I'm so confused Krektonix_Youtube 85 — 3y
0
It doesn't work dude. Krektonix_Youtube 85 — 3y
0
Sorry, put this in a local script in StarterCharacterScripts, obviously replace the part path to wherever you have the part. MendozaHM3SBulldog 32 — 3y
Ad

Answer this question