Object fitting system (2d array) Tetris style.

hello,
I am trying to make a system using a 2d array that will pick an object to put in a slot.
sort of like a tetris kind of fitting thing. I cant quite figure out how that would be done.
I know how to choose a random object I just need the part that checks if it can fit and then
if not pick a smaller one.

Could someone help, thanks

Huh? Tetris does not check if the current piece fit anywhere, it’s simply random. If the player has bad luck he looses because he can’t place the object anywhere useful. It’s not clear what exactly you’re asking here. Do you have problems picking an object at random or do you actually need help with the moving mechanics of the pieces ( i.e. can the piece move down here, can the user move it left / right without colliding somewhere).

You should ask more specific questions. I try to answer anyways…

Games like tetris are grid based (that should be clear since a 2d array is a grid). But that means each piece is made up of “grid cells”:

// the default tetris pieces
.......  .......  .......  .......  .......  .......  .......
.......  .......  .......  .......  .......  .......  .......
...O...  ...O...  ...O...  ...OO..  ..OO...  .......  ...OO..
...O...  ...O...  ..OOO..  ..OO...  ...OO..  ..OOOO.  ...OO..
..OO...  ...OO..  .......  .......  .......  .......  .......
.......  .......  .......  .......  .......  .......  .......

Those can be rotated around the center by 90°. All you do is rotating the cells according to the objects rotation and offset each cell based on the object position to figure out where to place each cell inside the grid. If you want to move a piece to the right for example, you just calculate each cells position for the new object position after the move and see if any cells overlap. If they overlap the move is not possible.

One approach would be to actually use GameObjects for each moving piece. Define a simple array for each piece that stores the relative gid positions of each piece.

So for example the first piece above could be defined by

(0, 1)     // #0   ..0..
(0, 0)     // #1   ..1..
(0.-1)     // #2   .32..
(-1,-1)    // #3

You can use Unity’s Transform.TransformPoint method to transform the local position into a global position. This would automatically account for the object rotation and the position of the object. So if this object is located at (10, 4) and rotated 90° counter clockwise you would get

    .................................
    .................................
   5.................................
    .........OOO.....................
    ...........O.....................
    .................................
    .................................
  0 .................................
    0    5    10   15   20   25   30

So the final positions of each cell are (9, 4) (10, 4) (11, 4) (11,3)