Search Unity

Random tile spawning

Discussion in '2D' started by Owzybrand, Oct 27, 2020.

  1. Owzybrand

    Owzybrand

    Joined:
    Apr 18, 2018
    Posts:
    2
    Please forgive me for my lack of understanding and knowledge!

    I am very new to Unity and C#, and I am looking to make a 2D Hexagonal Tile game for Android. I have absolutely NO IDEA where to start.

    My game will be a minimalist survival game where you have a home tile (center screen) that never gets destroyed or changed in any way, and 6 random tiles surrounding the home tile (these will be chosen from a random group of tiles). The idea is every time the player taps on a tile, the tile is destroyed after an event happens and a new random tile is spawned in the position of the old tile.

    I have no idea how to approach this in my script. I don't even understand how to find an individual tile's position! I know I need to use Tilemap.SetTile but after reading Unity Docs I am still very confused...

    Basically I need to know;

    1. How to set the tiles when the game starts
    2. How to destroy tiles when tapped
    3. How to replace tiles with random tiles

    Should I even be using tiles for this sort of game? Feeling very overwhelmed at the moment!

    Here is a picture of my scene currently so you can get an idea of what I mean (hopefully);

    Example Pic.png
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    You can certainly use a tilemap for this, although you might run into issues a bit later if you start doing complex stuff. My own game uses a isotilemap, but for my resource nodes I use individual game objects so they can have their own distinct colliders but I think if I really worked at it I probably could have made it work with just the tilemap.

    So, you have the gist of setting a tile correct, you use the settile function. To destroy a tile you just set the tile to null, although you may actually want to set it instead to an 'empty' tile that you create yourself (basically a black tile or similar). I'm not sure how the coordinate system for hex works, but guessing it is similar to iso which is a bit confusing. I found it best to create a quick script that loops through and just sets a bunch of tiles in a known pattern, like I set x 1-10 and y 1 and see what happens. That gives you a feel for how this works.

    Now how to do the tap, well that depends on what exactly your tap is. If you're just doing a mouse click, you can get the mouse position, and then convert that to a world position, and then ask the grid via the worldtocell function what the position of the tap was. My own game uses colliders instead as noted, but you don't seem to need to go that route. Do note, if you do experiment with colliders that the tilemap has 1 master collider instead of individual tile colliders which is why I didn't use it as it didn't tell me which tile was being hit. Anyway, once you have your cell position you can then do your destroy and recreate on that position. If it is the center cell, you just ignore the tap, or play a beep or something, but don't do the destroy.
     
  3. Owzybrand

    Owzybrand

    Joined:
    Apr 18, 2018
    Posts:
    2
    Nice, I love me some Isometric games! :)

    I'm still a little confused on the SetTile function to be honest, mostly in regards to the Vector3Int position. How do I get the XYZ coordinates of a tile? Or this what I find when I create a script that loops and sets tiles in a pattern? Sorry if that's a silly question. For the tap I will be doing a touch input, as this will hopefully be a mobile game! I'm assuming its the same thing only touch position instead of mouse position?

    Thanks for the super quick response!
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Yeah, if you're just getting a coordinate set from say the mouse position you do something like:
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Then you can use the tilemap or grid object's WorldToCell to give you the Vector3Int you're pointing at.

    The other way around, when you have a known position, you just create the vector3int yourself. 0,0,0 is generally right around the center of your screen, and X for iso goes up and to the right as I recall when it goes positive, while Y goes up and to the left (I could easily be mixing these up, that's why I recommend visualizing it yourself with some test scripts). I think hex works similarly, just each row is offset instead of uniform like iso is but always good to test so you can see how it works. Easy tests are make a loop solely incrementing X, then one on Y, then one doing both and see what patterns you end up with and that should show you what it looks like.