Search Unity

place objects on more than one cell in a grid based game

Discussion in 'Scripting' started by Garzec, Aug 20, 2019.

  1. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    I would like to create a grid based game with Unity. I generate the grid with this sample code

    Code (CSharp):
    1.      for (int x = 0; x < 10; x++)
    2.      {
    3.          for (int z = 0; z < 10; z++)
    4.          {
    5.              Instantiate(cubePrefab, new Vector3 (x, 0, z), cubePrefab.transform.rotation);
    6.          }
    7.      }
    I read the grid information from a file. The problem is that this code only works for objects having a size (width and length) of 1 cell. It might be that there are objects bigger than one cell.

    Example 1: The wooden benches



    Example 2: The flower pots



    How would you generate your map then?

    The first approach coming to my mind would be to connect the cells to a group. Whenever the player tries to move to or interact with a cell of this group, the group runs the logic. This could be a collision (player can't move here) or a trap (player dies on each group cell).

    But the second problem is, how would you place your models then? When generating an external level file, the data has to know that the model has to get stretched along with multiple cells, which rotation to use etc.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I would allow map cell to know what is inside it like a two method occupy(who) & leave(who), add size parameter to my objects, assume pivot is cell not object centered and then call occupy and leave for multiple cells based on my pivot's position and the size values for x and y.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Adding to the excellent points that @palex-nx posted above, and answering specifically your second question, what you want to do is create your building chunks with an extra script that defines their shape. This way the map placer can take any prefab and look specifically for that script, and query the script to find out how big the object is.

    The simplest thing would be a script with a single integer in it called "Size," where 1 would mean 1x1, 2 would mean 2x2, etc.

    A more complicated step would be to support width and height, so 1x3 would be possible. But keep in mind you will need to handle rotation by 90 degrees, possibly.

    Finally the most-flexible approach would be a setup that defines a grid pattern of "what cells do I occupy," and that would let you make Tetris-piece-shaped buildings, such as L- or T-shapes, again all by making this available in a common shared script (with different data) on each of your prefabs.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    To back up a little, you have a list of placed buildings. If you placed 2 fences and a barn, the list has those three things. Each building knows the square it's officially in, and how big it is. Maybe barns are 2x2 and are placed by their lower-left.

    When you place a barn at (4,9), the barn-placing code knows to also link the map to it at (4,10), (5,9) and (5,10). When you pick up the barn, barn code knows to set those 4 back to null. When you try to place a barn, barn code knows to check whether those 4 squares are empty.

    It's a bit of work, but easy to visualize and is pretty good practice with indexes.
     
  5. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    yeah, good ideas. Thank you very much guys