Search Unity

Calculate max num of obj that will fit in screen

Discussion in 'Getting Started' started by taylorMeow, Jun 20, 2022.

  1. taylorMeow

    taylorMeow

    Joined:
    Apr 16, 2022
    Posts:
    30
    I'd like to have a bunch of small balls enter the screen from the top left and then move side by side in a line to the right of the screen, drop down a bit then move to the left, then repeat the process "snaking" their way down to the bottom, right to left then left to right, lower and lower until the whole screen is filled with a conveyor belt of balls, so to speak. I've accomplished this, however to fill the screen so that there were no spaces or overlapping of balls I had to hard code the exact amount that fit. For my screen, that is. I'm not sure how to calculate this dynamically. I thought maybe get the world max x and y then divide by the ball width, but that doesn't seem to work. I feel like I'm missing an obvious way to do this. Thanks for any help.

    Code to instantiate:
    Code (CSharp):
    1. buffer = -100f;
    2.         worldMin = Camera.main.ScreenToWorldPoint(new Vector2(buffer, buffer));
    3.         worldMax = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width - buffer, Screen.height - buffer));
    4.         objectCount = 161;
    5.  
    6.         BeginFindTimer();
    7.         for (int loop = 0; loop < objectCount; loop ++){
    8.             spawnPosition = new Vector2(
    9.                 worldMin.x - (0.5f * loop),
    10.                 worldMax.y-1.5f);
    11.             GameObject newGO = (GameObject)Instantiate (objectToSpawn, spawnPosition, Quaternion.Euler(new Vector3(0, 0, 0)));
    12.         }
    Code to move balls:
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col){
    2.         rb = GetComponent<Rigidbody2D>();
    3.         rb.velocity = new Vector2(0, 0);
    4.         buffer = -100f;
    5.         worldMin = Camera.main.ScreenToWorldPoint(new Vector2(buffer, buffer));
    6.         worldMax = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width - buffer, Screen.height - buffer));
    7.         transform.position = new Vector3(transform.position.x, transform.position.y - 0.4752f, transform.position.z);
    8.         rb = GetComponent<Rigidbody2D>();
    9.         bool moveRight = false;
    10.         bool moveLeft = false;
    11.         if (col.gameObject.name == "RightWall"){
    12.             moveLeft = true;
    13.         } else if (col.gameObject.name == "LeftWall"){
    14.             moveRight = true;
    15.         } else if (col.gameObject.name == "BottomWall"){
    16.             transform.position = new Vector3(transform.position.x, worldMax.y-1.9752f, transform.position.z);
    17.             if (transform.position.x < 0){
    18.                 moveRight = true;
    19.             } else {
    20.                 moveLeft = true;
    21.             }
    22.         }
    23.  
    24.         if (moveRight){
    25.             rb.velocity = new Vector2(1,0);
    26.         }
    27.         if (moveLeft){
    28.             rb.velocity = new Vector2(-1,0);
    29.         }
    30.     }
     
    Last edited: Jun 20, 2022
  2. taylorMeow

    taylorMeow

    Joined:
    Apr 16, 2022
    Posts:
    30
    I guess just disregard. I decided to go another direction. The closest I could get was this:

    Y:
    5 (worldMax - 1.9752 offset) = 3.0248 + 5.45 (bottom half) = 8.4748

    X:
    2.58 + 2.58 (each half, no offset) = 5.16
    Sprite world size = 26.4 * 0.2 scale = 0.4752
    8.4748 / 0.4752 = 17.83
    5.16 / 0.4752 = 10.85
    17.83 x 10.85 = 193 (this can't be right bc that's way too many bubbles)

    Bottom border height: 1 * 0.2 scale = 0.1 (0.5 instead of 1?)
    5 - 1.9752 - 0.1 = 2.9248 + 5.45 = 8.3748 / 0.4752 = 17.62 (no significant difference when adjusting number for screen "walls")

    So I will move onto another idea, it seems we can't delete posts or I would. Sorry for wasting space.