Center Of Screen In World Coordinates

Is it possible to find what the center of the screen is pointing to in world coordinates?

For example, whenever the player moves the camera, I would like to know exactly what point (in world coordinates) the center of the screen is pointing to.

2 Likes

You could use ViewportPointToRay. To find the center of the screen, look at ViewportSize. By taking the X and Y sizes and dividing both by 2, you can plug them into ViewportPointToRay to cast a ray from the center of your screen.

Keep in mind the ray that ViewportPointToRay casts only has a magnitude of 1 stud, so if you want a longer ray you’ll have to cast your own ray with Ray.new() using data from the ray you got from ViewportPointToRay.

Using whichever ray you want out of the two, you can use FindPartOnRay to find where the ray intersects (or ‘hits’). The second value returned is where the center of the screen is pointing to in world coordinates.

10 Likes

Works great. Thank you so much!

local function ViewportPointToWorldCoordinates(x, y)
	
	local ray = workspace.CurrentCamera:ViewportPointToRay(x, y)
	local partHit, endPosition = workspace:FindPartOnRay(Ray.new(ray.Origin, ray.Direction*5000))
	
	return partHit, endPosition
	
end
5 Likes

This actually solved a problem I was having with ViewportPointToRay where I was setting depth and getting weird issues. Protip for future, don’t set depth if you wanna get where the mouse is pointing.