- Home /
Restricting camera to within areas. 2D Adventure Platformer
I'm working on a 2D Platformer and currently the camera just follows the character wherever, keeping him in the center of the screen. However, if I could restrict the camera to certain areas, i would know exactly what the player sees, rather than part of the camera going beyond the areas the character can reach. An example would be if I had two rooms above eachother, the camera would need to zoom in to stay within the bounds of that area, so as not to show anything in the next area above.
It's hard for me to describe exactly how I see it, so I made a picture.
I'm not sure if the easiest way would be to give all the surrounding platforms a certain tag that the camera would look for, or if I should mess with colliders...
Sorry if this is a stupid question.
Answer by Dizzyman572 · Jun 08, 2014 at 03:17 AM
Here's one trick that could probably work for you. Remove your camera as a parent of your character and then manually program your camera to follow your character's position along the X and Y axis. Your character should still be at the center of the screen. From there you can use if statements to tell your camera "If your position along the X axis is great than or equal to 10, do not stop follow the transform. If your position along the X axis is less than or equal to -10, do not follow the player." Then you just declare a few integer variables stating the maximum and minimum parameters. If you set these variables to be public, you should be able to tweak these variables to meet the needs of the level your making considering that you use different cameras for different scenes. I hope that helps.
Your answer
Follow this Question
Related Questions
2d camera movement 1 Answer
Improve smooth 2d side scroller camera to look more fluent 1 Answer
ScreenToWorldPoint not accurate 0 Answers
Cinemachine camera weird movement on Windows build 0 Answers
2d camera along fixed path 1 Answer