I’m making an ORB in a game and I don’t know how to make it so if your camera looks at it, it runs a piece of code [I will copy the code from something I already made]. This script obviously needs to be Local (Run for the player that looks at it), not for every player in the game. Please also tell me where I should put the script and if it is a local or regular script. Thanks!
How do I do it. I’m new to scripting and I don’t know what the script thing could be I have no idea. Anyways.
I have tried posting on scriptinghelpers.org and this guy showed me a code but it just deleted it from the Workspace even when I wasn’t looking at it.
if camera.CFrame == CFrame.new(Camera.Position, ORB.Position) then
-- code
end
I didn’t actually test this but you can give it a try, make sure that’s in a loop or something. I’m not the best at scripting so this probably wont work. Also, can you share a script with us?
It will need to be a localscript to be able to tell if something is on screen. You’ll want to see how to use Camera:WorldToViewportPoint (roblox.com) to get its return value if the object in question is on the screen or not. If it is, you’d shoot a remote event at the server if the server is the one executing the code, or within the same script if not.
Aaaaaaa, in all honesty I don’t know how to use Remote Events, Remote Functions, etc. But I have looked at that. It says it returns a Vector3 value which is not what I’m looking for.
It returns two values, the first of which is a Vector3 you don’t need, the second of which is a bool that says whether the location is within view or not. That’s the value you want.
I tried it out but I don’t know how to get that second value lol. Sorry if that’s really easy, I don’t even know where to put the “success” part of the code…
How do I do that? Please tell me as I have no idea. At this point I’m just making it not be a thing. I’ve seen it in another game and there should be a Camera:IsLookingAtPart(orb) or something. Ugh.
local Object = workspace.Part -- Example Object
local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local MaxDistance = 30
RunService.RenderStepped:Connect(function()
local DistanceFromCamera, IsOnScreen = Camera:WorldToScreenPoint(Object.Position)
if DistanceFromCamera.x <= MaxDistance or DistanceFromCamera.y <= MaxDistance or DistanceFromCamera.z <= MaxDistance and IsOnScreen then
print("You Are Looking At The Object")
--//--
--Code Right Here
end
end)
You’ll need to use Camera:WorldToViewportPoint to detect where it is in your screen. Use Camera.ViewportSize to find the size of the viewport, and then make sure that the viewport point is:
Not outside of the screen (this is returned by WorldToViewportPoint after the position, but it doesn’t account for the point being behind the camera)
Not behind the camera (positive Z position = in front of the camera, negative Z position = behind)
If you’re struggling with multi-assignment (tuples) then maybe this endeavor is beyond your scope.
Nonetheless, here’s an example.
local function tupleExample() --A tuple is just multiple values.
return 1, 2, 3 --Function returns three values (a tuple).
end
--Here we declare three variables and assign to them whatever is returned by calling the function.
local a, b, c = tupleExample()
print(a) --1
print(b) --2
print(c) --3
print(a, b, c) --1 2 3
print(a..b..c) --123