How to detect when characters are at the edge of the screen in a 2D game?

I’m trying to make a system that detects when the players in my game are at the edge of the screen so that I can disable their movement and prevent them from walking further off or in other words I want the players to be on screen at all times.

In this screenshot you can see both characters are half way off the screen and the goal is to not have that happen

I’ve tried a few methods one of them was to calculate the distance from the character to the screen using like mouse.ViewSizeX and mouse.ViewSizeY which worked until I realized you could just adjust the size of the screen which breaks it. The other method which I think is the way to go for this is to have a physical part and do some math to position it, however, I haven’t been successful with doing the math for this.

1 Like

Magnitude and camera size i guess

You could set up a boarder (part) in the game and when the player touches it it could fire the event that you wanted

Maybe have a part constantly in the middle of the screen, and have magnitude check if the player is a certain distance apart from it (relative to screen size), meaning they’d be offscreen?

I know I personally hate when people tell me alternatives to what I asked, because sometimes it just isn’t what I want and is useless input, but I’ll give a few just in case you can’t find a solution to your problem:

  1. Try zooming the camera in and out based on the magnitude between the two players so that both players are always on screen at once?
  2. Just have the camera zoomed out enough so that the entire map is visible in it’s entirety? I know that probably isn’t going to be the same effect that you’re probably going for with the classic 2D fighter approach though.

I guess if you can’t figure out anything else just make it so the screen can’t be scrolled and both players are limited to their screen.

Sorry if this doesn’t help much lol
The game looks really cool though

1 Like

You could be polling HumanoidRootPart in a loop to see if it’s on-screen, and if not, they must be at the edge or beyond.
Camera:WorldToViewportPoint ( (you’re interested in the second return, inViewport, the boolean for if they are or are not inside viewport)
If they’re locked facing the center of the screen you could also add another part to their character somewhere right behind them and poll that instead

1 Like

An issue I keep running into though is this there’s like a weird stutter/glitch before it fully stops although now that I think about it it might be because I’m tweening my camera with a time of 0.2

edit: yeah ok the tweening seems to be what causes it so

If the camera will always be facing that exact direction, you could set up an invisible wall. That wall could check if a player has touched it, and if it has, you could make it be that it disables their movement.

I don’t know if you’ve already gotten an answer for this, but for my 2D fighter, I had a part that stays in the midpoint of the distance between the two players. Then I created two borders and set their CFrames to always be at the position of the midpoint + or - a number of studs away for the left and right side. You can keep changing the number of studs apart until you get the borders to look like they’re at the edge of the screen.

1 Like

I ended up using the first method I came up with (using viewport size to calculate screen edge) and it works perfectly for my game. I’m not sure why the other method wouldn’t work for my game but it kept giving me weird results.