Check if object is near the center of the screen?

I’m aware of how to check if an object is on the screen with cam:WorldToScreenPoint but how can I check if it is close to the center of the screen?

2 Likes

What’s your definition of “center of the screen”? Within a certain amount of pixels from the centre, or within a certain distance from the centre relative to the total screen size (i.e. 1/10 of the screen size is counted as “close”).

1 Like

A certain distance from the total screen size. Basically I’m making it so if you look at an npc “in the eye” it triggers, however I don’t want it to be so you have to have your mouse on it.

Probably something like this if it makes sense? (position on-screen is within the green circle).

you could just check the distance from the screen point to the middle of the screen:

local Distance = (ScreenPoint - ScreenSize/2).Magnitude

and if the distance is shorter than a radius (preferably percentage of the screen size rather than set amount of pixels to compensate for different resolution screens) then it’s under that circle

You can get the absolute center position of the screen by doing:

local center = workspace.CurrentCamera.ViewportSize / 2

then you can find the difference of the position of the point returned from the function and check if the distance is less than or equal to the radius of the circle:

local center = workspace.CurrentCamera.ViewportSize / 2
local v = (point - center).Magnitude
print(v, v <= CIRCLE_RADIUS)
3 Likes