- Home /
How to stop the camera from going out of bounds in a 2D platformer?
I have a camera that follows the player, as a child object of the player. But when I move the player across the scene, I need the camera to stop so that it doesn't show what's outside the scene. Here's what I wrote so far in a Camera Script attached to the camera.
1.57 is the width of the full background.
void FixedUpdate ()
{
if (transform.position.x <= (1.57 / 2)
transform.position = new Vector2 (1.57 / 2);
}
If the camera tries to move outside the background I want it to be held back. But the script doesn't work at all...
Also the width and height of the levels varies a lot, so I would like to know how to just make the camera know that it isn't allowed to go farther than what the background shows. Like a huge invisible object in the background which covers the entire stage. If the camera tried to show what is outside of that box object, it would automatically be put back.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134443im_/https://answers.unity.com/themes/thub/images/avi.jpg)