Showing posts with label Grid System. Show all posts
Showing posts with label Grid System. Show all posts

Unity Tile-Based Ground

Tile-Based Ground – Unity3D (C#)

To create a tile-based ground, we need a simple matrix system. A (row X column) matrix will take a plane texture and draw it in each cell.

public GameObject plane;
public int width = 10;
public int height = 10;

private GameObject[,] grid = new GameObject[30, 30];

void Start () {
 for (int x =0; x < width; x++) {
  for (int y =0; y < height; y++){
   GameObject gridPlane = (GameObject)Instantiate(plane);
   gridPlane.transform.position = new Vector2(gridPlane.transform.position.x + x, 
                gridPlane.transform.position.y + y);
   grid[x, y] = gridPlane;
  }
 }
}

Tile-Based Ground – Unity3D (C#)

Unity Grid System

Grid System – Unity3D (C#)

Grid system is used mostly in RTS games. Your characters or buildings move on grids. You need a matrix to create a grid system

public float cell_size = 2.0f;
 
private float x, y, z;

void Start() {
 x = 0f;
 y = 0f;
 z = 0f;
 
}

void Update () {
 x = Mathf.Round(transform.position.x / cell_size) * cell_size;
 y = Mathf.Round(transform.position.y / cell_size) * cell_size;
 z = transform.position.z;
 transform.position = new Vector3(x, y, z);
}

Grid System – Unity3D (C#)