Search Unity

Question Camera to cover entire screen?

Discussion in 'Cinemachine' started by scryedzxp, May 1, 2023.

  1. scryedzxp

    scryedzxp

    Joined:
    May 14, 2013
    Posts:
    47
    I created a new Android 2D mobile project set to the following resolution:



    In my scene, I have three 2D sprites and a camera as shown below:

    upload_2023-5-1_16-42-28.png

    As you can see, the sprites are not being rendered because they're outside of the camera. I would like the camera to scale and fit to any screen resolution so all of the sprites will be rendered.

    I was told there might be a way to do this with cinemachine, so that's why I'm reposting my question here.

    I know I can manually adjust the Position and Size fields of the camera so it covers the entire screen (Transform.Scale doesn't seem to do anything):



    but how can I do this through cinemachine or a script so that it accounts for all resolutions/aspect ratios?

    I tried using the following solution (2d - Scale camera to fit screen size unity - Game Development Stack Exchange) but that didn't work for me.
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Another option might be to put invisible objects in each of the top-left and bottom-right corners of the screen. Then create a CinemachineTargetGroup, add those objects to the group, with weight1:

    upload_2023-5-3_11-59-17.png

    Then, use that group as a follow target for your CM camera with a FramingTransposer in the Body section. Set the Group Framing Size to 1. The camera will automatically try to include both those objects on screen.