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

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

What? But then it’s in the same table as the Orb? Wouldn’t it also detect the player and be like, “You’re looking at the orb.”

It isn’t even working now. Whenever I look anywhere, (same for the orb) it just pops up saying “You are looking at the object”

No. The raycast is for checking if there is anything in the way. Excluding the player from that will not change the prior checks that specifically detect where the orb is on your screen.

1 Like

That doesn’t even make sense. Here’s the script:

local Object = workspace.SpecialOthers.TheORB -- Example Object
local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")

local MaxDistance = 100

--raycast
local params = RaycastParams.new()
params.FilterDescendantsInstances = {Object}
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true

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

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
		if result == nil then
			print("You Are Not Looking At The Object") 
			--Code Right Here
		else
			print("You are looking at the object")
		end
	end
end)

Okay it makes perfect sense and here’s why.

First you check if the orb is on-screen using WorldToScreenPoint. You check if the orb is on-screen. Not the character. The character doesn’t matter.

Then you check if there is anything blocking the orb on-screen. You have already checked if the orb is on-screen. Not the character. However by filtering descendants, you are just saying “the character doesn’t block the orb”. Note that you’re not detecting if the character is on-screen. You’re simply allowing the raycast to pass through the character, so that the character does not count as a wall that can block the orb.

Does that make sense?

1 Like

For this, as I said, it just keeps printing “You are looking at the object” whenever I look at ANYTHING

That’s because and has higher specificity than or. You probably want:

if (DistanceFromCamera.x <= MaxDistance or DistanceFromCamera.y <= MaxDistance or DistanceFromCamera.z <= MaxDistance) and IsOnScreen then

notice the parenthesis

Also, you only need to check the Z value of the Vector3.

if DistanceFromCamera.Z > 0 and DistanceFromCamera.Z <= MaxDistance and IsOnScreen then
1 Like

Ok, that fixes the printing when I look at anything, but there’s still 1 problem left.

It prints “You are looking at the object” through a wall even though I’m doing a raycast?

Because you’re doing the raycast but not checking the result at all. Revised, the if statement should be

if DistanceFromCamera.Z > 0 and DistanceFromCamera.Z <= MaxDistance and IsOnScreen and not result then

note the and not result.

1 Like

Now nothing prints. Huhhhhhhhhhhhhhhhhhhhhhhhh

Make sure the orb isn’t made up of multiple parts. If it is, you might have to ignore Object.Parent rather than just Object.

1 Like

Nope, it’s just a Sphere Part.