Search Unity

Physics for grid/block based game

Discussion in '2D' started by emmapp, Nov 19, 2013.

  1. emmapp

    emmapp

    Joined:
    Oct 5, 2013
    Posts:
    4
    I have a game with blocks that stack up to form a grid. I've a prefab with a Rigidbody2d and box collider2d which I'm reusing for each block, they get skinned with a different sprite.

    When some blocks at the bottom get removed, I need the above blocks to fall straight down - but they're moving ever so slightly to the left or right, and I'm getting gaps :(

    How can I achieve this? I've set angular drag to 0 and enabled 'fixed angle' but I don't know what else to do!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I wouldn't use physics for this. Use an array to represent the block positions, and move them manually.

    --Eric
     
  3. emmapp

    emmapp

    Joined:
    Oct 5, 2013
    Posts:
    4
    Ha, funny you should say that... I actually did that first (a couple weeks ago) and then since the new 2d stuff came out, I thought it'd be better to do it properly with game objects rather than just doing GUI.Box a lot!

    Apparently not!

    I guess I was hoping there'd be something like 'is kinematic' but in one dimension, or a way of locking the x value... Or something.

    Anyway, thanks for reply!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It is proper to do it with GameObjects, but it would still be a good idea to use an array to represent block positions, and move them manually.

    --Eric
     
  5. emmapp

    emmapp

    Joined:
    Oct 5, 2013
    Posts:
    4
    When I was doing the drawing with GUI.Box it was really easy to fit the grid to the screen size, whatever it was - I just divided the width and height number of blocks I needed in each dimension.

    Not really sure how this works with game objects - having trouble getting the grid to fill the screen. Do I set the screen res or is it controlled by the camera position? What's easiest?
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  7. emmapp

    emmapp

    Joined:
    Oct 5, 2013
    Posts:
    4
    Wow that looks perfect! Thanks so much :)