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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tiles like SimCity?

Discussion in 'Scripting' started by busterlock, Jan 6, 2016.

  1. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    Hey, human beings! Picture a game like SimCity or Theme Hospital, where you can build certain structures on top of predefined tiles, and each building has a "size" based on how many tiles are they going to occupy. Well, how would that be reached in Unity? My game at first it wouldn't need to have any other structures built other than the ones pre-generated in the level, but now I'm changing my mind about it, and I hope you guys could shed me a light :(.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    This is a big topic, and with such a general question we can only give you a vague answer. Some of the problems you will have to solve:

    1. Tiles themselves. You will probably want to create a Tile class (which may or may not be a MonoBehaviour) that knows, at minimum its position and what object if any occupies it. You'll then need to generate a 2-D array of these objects.

    2. Mousing over. You will most likely want to raycast to figure out where the player's mouse is. Then your tile manager script can convert that position into an equivalent array index.

    3. Buildings. They'll have a width and height, and the manager will have to know that when you place a building at (3,3) and it is 2x2, it also needs to notify cells (3,4), (4,3), and (4,4) that they are occupied.

    Each of those pieces is a fairly big job, and we're not going to write the whole system for you - but this should get you started. If you can get started on these, just come back here when you hit a snag.
     
  3. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    If it were me, I'd start by doing something simpler -> all buildings take up exactly one tile on a grid.

    Get a game working with that implementation, and I assure you you'll have picked up the skills to implement something that spans multiple grid spaces.

    Otherwise, as much as I want to help you out, it feels like you're trying to jump way too far ahead, and that makes it impossible for us to really help you. As @StarManta pointed out, this is a big topic.

    I'm happy to sit and discuss it, but you need to bring a little more to the table, at least try to get an implementation running so we have something to start with. Good luck!
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Good advice. It's not too difficult to convert a single-tile-only system to one that allows multiple tile coverings, so that makes a good milestone target.
     
  5. busterlock

    busterlock

    Joined:
    Sep 26, 2015
    Posts:
    58
    @StarManta and @apsdsm thank you for your replies! Well, it seems obvious now that I pulled a classical newbie move, the "I bet it's not that hard" one. I will probably try the second tip first since it's easier and this is my first game so no need to try and get all Metal Gear Solid on it, but I will sit down and study more to make it the better possible. Thank you, young masters!