Search Unity

Grid Based System like SimCity

Discussion in 'Editor & General Support' started by KKS21199, May 27, 2015.

  1. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    What is the best way to have a grid based system in Unity ? Like to place building on them and etc

    Unity is the best engine to do any kind of game. So I am sure it's possible. I just don't know where to begin. The things I am pretty sure about right now is having a grid on the Land (any mesh) and then using Physics.RayCast for mouse clicks to know where to instantiate the other mesh.

    But I am not sure how to generate the grid for the land, what are the possible ways ? Not exactly for the player to see the grids, but for snapping the object to the grid. I would love to hear some ideas so I could script them
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Well, as with any programming challenge, there are nearly infinite ways to do this. Nobody can try without failing, and if you're not failing, you're not trying hard enough, so my advice is to just try something even if you don't think it'll work (you'll learn something!)


    Here are 2 methods I might try starting with if this were my project:

    1) make a prefab for a "tile" that you can place buildings on and replicate it. You can make different kinds of tiles like water and grass. The downside to this approach is that it will be very difficult to make buildings that take up more than 1 tile.

    2) have your world laid out how you want, then make a gameobject and place it somewhere for it to be the origin for your grid (the world origin would work too). Then when you find your point on the grid plane (using raycasts), you can use modulus to find what tile you're in. (if your tiles are 20x20, you can take your selectedPosition.x % 20, selectedPosition.y % 20 to find which 20x20 tile your in. Don't forget to offset your selectedPosition by the origin!)


    Most importantly: the way things are done should be dictated by the type of game you're playing. For example, my method 1 would work great if you only plan on buildings taking up 1 tile each, but that won't work for most games.
     
    KKS21199 likes this.
  3. KKS21199

    KKS21199

    Joined:
    Nov 22, 2012
    Posts:
    139
    Screenshot (4).png
    This was my first trial, I was able to calculate the raycast hit point and round it up to place it in a grid. [left 1.5 as grid].
    What I am trying to do is to draw the grid on the mesh [the mesh doesn't exactly has to be straight without up and downs] so I am trying to draw the grid lines.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Well, you could make the grid a part of the texture, but the best way to do this would be to use a projector. Imagine it like a real projector: you cast an image onto a surface, so you'd be able to "project" your grid onto your ground mesh.

    Unfortunately I don't know how to use projectors, I only know they exist and what they do. Good luck!
     
  5. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132