Search Unity

How can I create a Grid with Unity?

Discussion in 'Getting Started' started by Sotoca-Estruch, Aug 20, 2016.

  1. Sotoca-Estruch

    Sotoca-Estruch

    Joined:
    Aug 20, 2016
    Posts:
    8
    Hello everyone, I want to make a game like Fire Emblem with Unity. To those people who don't know that game, I'll give you a link where you can see a bit of the gameplay:

    http://fireemblem.nintendo.com/gameplay/

    The thing is that I don't know how to make a board or a grid in the scene, I have a small idea, but I don't know how can I apply it to Unity. I have been thinking about this and the most easy thing I can think is to create a plane in the scene and put a script with a matrix of Game Objects here, that matrix should be the grid of the game, but the thing is that even if I do that, I don't know how to show the matrix in the plane. How could I make a grid floor?

    Some people recommended me the Grid Framework plugin, but since this is a game for school, I don't want to spend money on plugins, another point is that I'm completly a beginner in Unity, I have made the roll-a-ball tutorial, another game of blocks, an infinite runner and the survival shooter, so I have less or less an idea of how Unity works but I'm still a beginner.

    Thank you very much.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    As a beginner, I'd recommend you just lay out a bunch of textured Quads for your board squares. (Or if it's a 2D game — which it probably should be, since you're new — use a bunch of Sprites instead.)
     
    Kiwasi likes this.
  3. Sotoca-Estruch

    Sotoca-Estruch

    Joined:
    Aug 20, 2016
    Posts:
    8
    Yes, that was what I was thinking, but how can I connect those sprites with each square of the matrix? Is a good idea making a script who has static matrix and add it to each sprite of the scene? I don't understand very well how can I make a grid-based scenario.

    Thank you for reply.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You can just use math. To make the math easy, arrange your grid so that the squares are 1 unit apart, with the lower-left grid square at 0,0. Then, given any sprite, the grid column is Mathf.RoundToInt(transform.position.x), and the grid row is Mathf.RoundToInt(transform.position.y).

    Converting in the other direction is even more trivial: transform.position = new Vector2(column, row). (Or maybe new Vector3(column, row, z), if you want to use a nonzero z value.)
     
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    The grid is usually just one or more two-dimensional arrays in layers in a tile-based game. One for map tiles, one for units (and checking if the spot can be moved to) plus any other layers which make sense. Placement of sprites needs to be translated from the grid position to the appropriate world space position, which should be pretty easy.

    The grids don't need to actually hold sprites either, just an ID for the type of tile or unit occupying the space. Whether you do this in 2D or 3D the general logic is the same (for 3D you just use models instead). Type can mean just general terrain type, and map generation could randomise it from a selection, perhaps applying summer/winter sprites for seasons. With a type you can quickly look up the information you need to determine movement costs too.
     
  6. HamzaGzbyk

    HamzaGzbyk

    Joined:
    May 22, 2020
    Posts:
    1
    ı dont ad grid pls help me...