0
\$\begingroup\$

I tried to search different solutions but also due to the lack of knowledge in camera handling I found none fitting my needs.

The situation is the following.

I have a 2d game structured in rectangular rooms and I want the camera to always have the whole room visible in the screen despite the screen resolution and the room bounds.

Any clue?

Edit: Sorry for really bad painting, this is what I'd like to achieve somehow.

preview

The camera viewport is where, inside the screen, the game will flow. everything else will be static, like a GUI frame with general informations and commands.

\$\endgroup\$
  • \$\begingroup\$ You didn't give any snapshot, any code, even not a clear idea that what you are trying and what is happening. Please add some clarity in you question that what is your scenario, what sort of assets you are using and what you actually want? Snapshot always give the better idea to help someone \$\endgroup\$ – Hamza Hasan Feb 21 '16 at 21:15
1
\$\begingroup\$

You can't restrict the camera to a rectangular area in all resolutions/aspect ratios without either distorting the image.. or cropping out the area around it.

If cropping is the way you are looking to go, take a look at:

http://www.jgallant.com/restricting-2d-camera-zoom-and-movement-to-an-image-in-unity-csharp/

\$\endgroup\$
  • \$\begingroup\$ That's interesting, @Jon Can cropped parts of the screen be used to display something else, anyhow? \$\endgroup\$ – Leggy7 Feb 22 '16 at 22:47
  • \$\begingroup\$ @Leggy7 yes of course. The idea is to fill the area so that you never see a background that is outside the camera bounds. The technique I am providing, crops from inside the camera shot, so it will ensure that you will always be inside the defined area \$\endgroup\$ – jgallant Feb 23 '16 at 0:39
  • \$\begingroup\$ Sorry for my confusion, I update my question to ask clearlier how to enclose a camera into a larger view \$\endgroup\$ – Leggy7 Feb 23 '16 at 10:12
  • \$\begingroup\$ Sorry again: I didn't think enought before updating. My further request can be achieved via GUI I guess. Your answer is fine. Thanks \$\endgroup\$ – Leggy7 Feb 23 '16 at 11:01
  • \$\begingroup\$ @Leggy7 Ok. The code provided should be able to do what you are trying. You will need to possibly remove the resizing portion of the code, if you want to retain a certain size. In LateUpdate(), you could just check to make sure the camera is still inside of the Bounds, Regardless, the script gives you the world Bounds, so you can easily enforce the area that the camera is allowed to wander in. \$\endgroup\$ – jgallant Feb 23 '16 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.