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

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?

The raycast ensures that there are no walls between you and the orb.

1 Like

Yeaaah as I said I don’t know how to do raycasts. I’ve watched a video for it but not from a camera look vector or whatever, and I never saw the .FilterDescendantsInstances and stuff.

local params = RaycastParams.new()
params.FilterDescendantsInstances = {orb}
params.FilterType = Enum.RaycastFilterType.Blacklist

local camPos = workspace.CurrentCamera.CFrame.Position
local result = workspace:Raycast(camPos, orb.CFrame.Position - camPos, params)

-- if result is nil, then the orb is visible
1 Like

Let me put some code together and see if it works

I think it’s detecting my character… How do I stop that

But everything works, I put a

if raycast == nil then
print(“Looking”)

How do I stop the raycast from detecting my character?

Put the character in FilterDescendantsInstances as well.

1 Like