Detecting when the player reaches the edge the edge of their screen?

is it possible to detect when the player reaches the edge of their screen? I feel like everything I’ve tried was useless because the player can just resize their screen breaking the code lol.

I’m trying to detect when they reach the edge of the screen because I wanna disable their movement so this doesn’t happen

Here’s my current code (it was disabled for the video)

function WorldToScreen(Pos) --This function gets a World Position (Pos) and returns a Vector2 value of the screen coordinates
	local point = workspace.CurrentCamera.CoordinateFrame:pointToObjectSpace(Pos)
	local aspectRatio = Mouse.ViewSizeX / Mouse.ViewSizeY
	local hfactor = math.tan(math.rad(workspace.CurrentCamera.FieldOfView) / 2)
	local wfactor = aspectRatio*hfactor

	local x = (point.x/point.z) / -wfactor
	local y = (point.y/point.z) /  hfactor

	return * (0.5 + 0.5 * x), Mouse.ViewSizeY * (0.5 + 0.5 * y))

local Vec2 = WorldToScreen(HRP.Position)
if Vec2.X <= 310 or Vec2.X >= 1090 then
	if (UIS:IsKeyDown(Enum.KeyCode.D) and Character.CurrentFacingDirection == 1) or 
		UIS:IsKeyDown(Enum.KeyCode.A) and (Character.CurrentFacingDirection == -1) then

This code basically works as long as they don’t resize their screen so how can I make this more flexible I guess?

1 Like