Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Orthographic Camera fit problem

Discussion in 'General Graphics' started by unity_2A376606C9FD63AE58B1, Sep 2, 2023.

  1. unity_2A376606C9FD63AE58B1

    unity_2A376606C9FD63AE58B1

    Joined:
    Aug 3, 2023
    Posts:
    5
    I create random sized 2d grids and set the camera to middle of the grid, I also try to understand orthographic size and adjust it to fit whole grid but I guess I fail, Here are the code snippet and the fitting problem:

    I would also be appreciated if you have any other advice for my programming and design style.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEngine.WSA;
    6.  
    7. public class Grid2D : MonoBehaviour
    8. {
    9.     [SerializeField] private int _width, _height;
    10.     [SerializeField] private Tile  _tilePrefap;
    11.     [SerializeField] private  Camera _cam;
    12.  
    13.     void Start()
    14.     {
    15.  
    16.         GenerarteGrid();
    17.         SetCamera(_width,_height);
    18.  
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.        
    24.     }
    25.  
    26.     private void GenerarteGrid()
    27.     {
    28.         for (int i = 0; i < _width; i++)
    29.         {
    30.             for (int j = 0; j < _height; j++)
    31.             {
    32.                 var tile = Instantiate(_tilePrefap, new Vector3(i, j, 0), Quaternion.identity);
    33.                 tile.transform.parent = transform;
    34.                 tile.name = j + " " + i;
    35.  
    36.             }
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42.     private void SetCamera(int x, int y)
    43.     {
    44.         _cam.orthographicSize = (float) _height / 2 + 0.5f ;
    45.  
    46.         _cam.transform.position = new Vector3((float)x / 2 - 0.5f, (float)y / 2 - 0.5f, 0);
    47.     }
    48.  
    49.  
    50. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Start with the docs for Camera. By default it is half the visible world height.
     
  3. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    your screen must be the same resolution as the grid if you want to fit it

    in this case your grid seems 8:10, so do you want 8:10 resolution?
     
  4. unity_2A376606C9FD63AE58B1

    unity_2A376606C9FD63AE58B1

    Joined:
    Aug 3, 2023
    Posts:
    5
    I actually want my screen to be 9:16, and regardless of the dimension of grid I want to fit it in my screen.
     
  5. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    then you will need to make a 9:16 grid