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 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.

2 Likes

you could do something like

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?

https://www.roblox.com/library/8511705127/CameraFocus

This doesn’t work, as it detects the ORB through walls and stuff.

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.

2 Likes

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.

2 Likes

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…

if your gonna force shift lock for the play you can just do mouse.target and if its the orb then do your code

I’m not doing that though?


This doesn’t work at all. I don’t know what you’re trying to achieve if somehow the camera and the orb are on the exact same position.

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.

you can use WorldToScreenPoint

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)

this needs to be on client so youll need to use local scripts

just use remote events to connect it from the server

try raycasting from the camera? See if the ray hits the orb thing

Every frame, via RunService:BindToRenderStep or similar, execute some code to check if the orb is visible.

  1. 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)
  2. Next, you’ll need to perform a raycast from the camera position to the orb to make sure it’s not occluded by other objects. You can use RaycastParams.FilterDescendantsInstances with FilterType set to RaycastFilterType.Blacklist to exclude the orb itself from the raycast.

  3. If the viewport point is on-screen and the raycast did not hit anything (the path to the orb is clear), then the orb is most definitely visible.

2 Likes

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

The “Programming in Lua” guide has a section on this as well.
https://www.lua.org/pil/5.1.html

Don’t know how to do that and it detects it through a wall which renders it useless :frowning:

That’s too advanced for me, I have no idea what to do there.

Well that’s just how it’s done. There’s no event for when something enters/leaves the screen. So you have to assemble your own solution manually.

This is how scripting works.

1 Like

There’s a script that works but it goes through objects, how do I fix that instead?