Is there a way to see if a player is looking at something?

Hello there,
I was wondering if it was possible to see if a player is looking at a part (in his screen)

1 Like

I think you could use Camera:WorldToScreenPoint() using the position of the thing to check if the can see it as the argument, and using the 2nd return of WorldToScreenPoint, which detects if the position is in the bounds of the screen, you 'd probably need a localscript

Keep in mind that this does not do any raycasting, so if it’s behind parts or terrain, it’ll still detect it if it’s on your screen, so you’d probably have to do some raycasting in conjunction with this function

1 Like

But what you’re talking about is a point on the screen,
I’m talking of a part like if the part is visible on screen

Looking it up, others also use WorldToScreenPoint in combination with magnitude checks to detect if something in visible to the player’s screen

Should work fine for your usecase as well, I believe

Are you referring to the player’s head or first person?
If so, try this:

local Head = Player.Character.Head
local D = Head.CFrame.LookVector * 20
local RayCast = workspace:Raycast(Head.Position,D)
if RayCast then
    print(RayCast.Instance)
end

Define the player and if he is further away, change 20 for a larger number.
Source: Raycasting | Roblox Creator Documentation

That casts a fine line from the player’s head, so it wouldn’t work. He’s talking about ALL the objects in the camera view. (It would work in an FPS or something though for bullets)

The second argument of WorldToScreenPoint is whether or not it is visible on screen.

I’m making an horror game, and when you look at the creature a jumpscare would play but I have no idea on how to get if the player has the creature visible on his screen (even slightly)


This is what you want, worldToScreenPoint