So I have four "boards" made of 16 sprites (4x4). Each board is parented to an empty GameObject. I would like to randomly generate their positions but I am not sure how. However, they should still be generated at the middle of the screen not like anywhere off. So basically each board can be placed at any position but must touch at least one of the boards. The sprite of one board should line up with the sprite of the other board. For example:

enter image description here

So yeah any help is appreciated!


1 Answer 1


Notice that for each side of a board there are 7 places to place another board. (To see all the possibilities place a board so that only one square touches the other board, then slide the first one along that side.)

So you can place the first board in the center to get started, then randomly choose a side and then choose one of the 7 positions. Now it gets a little tricky. We can randomly choose a board and then a side and then a position, but we don’t want overlaps so we need to check for those and pick again if the board would overlap.

You can use a Dictionary<string, boolean> to keep track of which spaces are taken by taking the integer coords and concatenating then together with a comma (or whatever separator), and storing true in the dictionary for all the taken spaces.

So if you do that, then you can generate a potential place to put a board, check if it is taken (that is, if any of the squares it would cover are already in the dictionary,) and if not, place it and add its squares to the dictionary. Otherwise try another place.

You may also want to keep track of the spaces you have already tried so you don’t end up trying them more than once.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .