Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Grid Placement System

Discussion in 'Scripting' started by Kraze, Dec 28, 2014.

  1. Kraze

    Kraze

    Joined:
    Jan 6, 2013
    Posts:
    13
    Just wondering what the best way to implement a grid system like the ones used in game like Clash of Clans, Boom Beach, Simpsons Tapped Out etc. Every tutorial that I have found has used hundreds of planes to represent each individual cell but I don't think this is the most efficient way.

    Any help is appreciated.
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Basically, you just have to calculate on which cell you are when a gameObject / your mouse / whatever is at a position X, Y.

    If the size of your cells is w:100 / h:100 for example, and a gameObject is at the position x:1542 / y:230, you just have to do a simple calcul :
    cell x : x / w : 1542 / 100 : 15
    cell y : y / h : 230 / 100 : 2

    Something like this.
     
    wbakunis and Kiwasi like this.
  3. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    What would be a smart lightweight way of creating these cells from the start? I'm looking to have more than 100 cells on multiple floors / levels. Did something like what @Kraze mentioned with creating multiple planes. This is a heavy and unintuitive option.

    Screenshot of generated cells
    https://www.dropbox.com/s/oaugw1x1gfa7min/Screenshot 2014-12-29 16.53.23.png?dl=0

    Code (CSharp):
    1. void Start ()
    2.         {
    3.                 gridplacementOBJ = GameObject.FindGameObjectsWithTag ("DevGrid_Collider");
    4.                 grids = GameObject.FindGameObjectsWithTag ("DevGrid");
    5.                 GridGenerate ();
    6.         }
    7.  
    8.         void GridGenerate ()
    9.         {
    10.                 for (int x = 0; x < 20; x++) {
    11.                         for (int y = 0; y < 5; y++) {
    12.                                 for (int z = 0; z < 20; z++) {
    13.                                         Instantiate (gridprefab, new Vector3 (x * 5, y * 5, z * 5), Quaternion.identity);
    14.                                 }
    15.                         }
    16.                 }
    17.         }
    18.  
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Why do you need to create these cells ??
     
  5. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    A few of my project ideas are "management" type games exactly like Prison Architect. I'd like to be able to place things on grids and then be able to save / load it during runtime. AI won't be grid movement though.
     
  6. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Okay, but still why do you want to create them ALL at the beginning ?

    In Prison Architect, the plane (= ground) can be a texture that shows "fake cells"* and when you want to create a "wall" you create a game object with the texture of the wall.. In this way, you don't have to create 1000 cells at the beginning. The only thing is when you'll load a game (which will contains a hundred of cells, maybe more (but not 1000 except if the player fills all the "fake cell" and it shouldn't happen all the time)) ; you should have a loading page..

    Do you understand or ?

    Edit: another example: if you want to highlight the cell where the mouse his, you just need to have a "plane" that follows the mouse and depending on the real position, you determine the cell position.

    Edit²:
    * I don't know if you want to show the cells (the borders) to the player, if not, you just need a grass texture (or whatever). But if you need to show the cell, you should draw lines.
     
    wbakunis likes this.
  7. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    Yea I understand perfectly. I'm not directly trying to make them appear at start. I just put the code in Start() to see if they would appear for testing. My only confusion is where these actual cells come from? Do you just put an empty gameobject on the map and give it a script to generate individual hidden points? How does one get the fake cells to coincide with the actual cells so that they aren't offset?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The cells don't exist. You simply have a function that converts a world location to a cell location, and vice versa. If you really need a representation of the cells then an array is typically sufficient.
     
  9. Delak

    Delak

    Joined:
    Jan 8, 2013
    Posts:
    6
    Sbizz I am trying to do some very close to that, could you point me in the right direction as to what I need to read/learn and/or tutorials that talk about it?

    Thank you
    Delak
     
    wbakunis likes this.
  10. wbakunis

    wbakunis

    Joined:
    Jan 28, 2014
    Posts:
    50
    Yea, im still trying to figure this out too. I was able to find this on the unity forums. If I understand it right, it just rounds the location off. I deffinitely need a better tutorial / explaination.

    Code (CSharp):
    1. var currentPos = transform.position;
    2. transform.position = Vector3(Mathf.Round(currentPos.x),Mathf.Round(currentPos.y),Mathf.Round(currentPos.z));
    Edit: Another link
    http://forum.unity3d.com/threads/c-procedural-mesh-generation.245143/

    Maze "generator": Links shows how to create cells though.
    http://catlikecoding.com/unity/tutorials/maze/
     
    Last edited: Jan 8, 2015
  11. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I attached a small sample of a grid generation.

    It's just to show you how you can handle a grid without have a game object which represents each cell.
     

    Attached Files:

    • grid.rar
      File size:
      136.7 KB
      Views:
      794
  12. Delak

    Delak

    Joined:
    Jan 8, 2013
    Posts:
    6
    Thank you Sbizz, was able to load that up and test it out. I will be spending lots of time looking at the code.
     
  13. Delak

    Delak

    Joined:
    Jan 8, 2013
    Posts:
    6
    Looking at the code, I think I understand more than I thought I would. But how do you setup the CellItem. I am assuming this will randomly generate different cells?
     
  14. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I don't really know why I created a CellItem class. You could just save the GameObject. But if you want to identify each "items" dropped on the cells, you should have a class which defines these items.

    Something obvious: each "item" should have a name and maybe a cost. So, you could have an abstraction called "ACellItem", with a string and an integer and then, you just have to create other classes which inherit from "ACellItem"

    Code (CSharp):
    1. public class ACellItem {
    2.     public abstract string called {get ;}
    3.  
    4.     public int cost = 1;
    5. }
    6.  
    7. public class Wall : ACellItem {
    8.     public override string called { get { return "Wall"; } }
    9. }
    10.  
    11. public class Gate : ACellItem {
    12.     public override string called { get { return "Gate" } }
    13. }
    It depends on what you want on your game.

    PS: I do not instantiate randomly "item" ; I have a variable which tells me which item should I instantiate when the user clicks (_cellTypeChosen, something like that).