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

Question 2D Platforms with rule tiles and 3-4 blocks repetition patterns

Discussion in '2D' started by beethanmaxwell, Oct 15, 2023.

  1. beethanmaxwell

    beethanmaxwell

    Joined:
    Oct 15, 2023
    Posts:
    3
    I'm looking into making a simple platform game with many long platforms.
    To avail making boring I picked around/built some tiles which don't repeat each tile but instead have a more varied approach by having multiple patters which repeat every 3-4 tiles.
    Everything works ok when placing the tiles by hand but I'm looking into using rule tiles to automate the job and I don't see how to obtain this.
    Basically I'd for these tiles:
    • L, R : the left-end and right-end of the platforms
    • 1: a tile which matches the border of L and R
    • 22: a couple of tiles where the leftmost one blends with the L tile and the rightmost one blend with the R tile. Cannot place a single tile because the picture will not match the border
    • 333: same as 22 but with a longer drawing
    • 4444: same as 333 but with a longer drawing

    using a tileset as the above I can create long platforms which don't like to boring, like:
    • L221333224444R
    • L444412233322R
    • L333444422333R
    • etc...
    so my question is:
    • is there any easy way to attain this using Rule Tileset?
    • any other rule-like tileset that you can point me to that does this?
    • if none of the above: do you believe it woul be very complicated to implement the above? Any pointer to some example source code from where to start?
    BTW: I'm using 2D Tilemap Extras 3.1.1 on Unity 2022.3.10f1

    p.s.: as you might notice the above model allows multiple options when forming long platforms and I'd definitively want that to be randomly picked in order to attain variety!
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    150
    No/not that I know of

    You'll have to write your own custom rule tile, you (probably) need override GetTileData method to choose the random output based on your pattern and the RuleMatch method to check if
    the second/third/fourth 4 has a neighbor at position L etc. You'll have to enable extend neighbor option to access second/third/fourth neighbor information on the rule tile
    You can see the default implementation of the above mentioned methods in the RuleTile.cs script under Unity.2D.Tilemap.Extras namespace/project. You can access them through these steps:
    upload_2023-10-16_12-49-40.png
    (I cant count apparently)

    Just google "unity custom rule tile" and you'll see lots of tutorials.

    I wouldn't recommend implementing such a "complex" behavior because I doubt the patterned repetitions will look nice. If you are using the tiles as platforms to jump on etc. why not use regular gameobjects for platforms? At least you'll be able to add custom behavior using regular scripts too.

    You can also attempt to make tileable rules like the "desert sand wave" rule, which only uses 3 rules. These tiles are placed on a tilemap that renders above the actual base tilemap.
    upload_2023-10-16_12-57-11.png

    Cheers
     
    simoneavogadro likes this.
  3. LoupAndSnoop

    LoupAndSnoop

    Joined:
    Jun 23, 2023
    Posts:
    34
    You can definitely do this with custom code. I made my own custom ruletile following a Youtube guide.

    Method 1: (this lets you super customize logic) Look into the ruletile source code. There is a virtual method called RuleMatch which is a function of an int (the rule index, basically) and the tilebase. We are looking for the earliest virtual method in RuleTile that calls RuleMatch, and we want to override it to give us the tile coordinate we are looking at. Specifically, instead of calling RuleMatch(int, TileBase), we want it to call our own personal RuleMatchAlternate(int, TileBase, Vector3) function that also gives us a coordinate of the tile we’re looking at. Then, you can code your own rulematch method to do whatever you want as a function of tile position by using tilemap.GetTile at a given coordinate that is a certain position away, and use that info to do more advanced logic.


    Method2: Write a class that searches a whole tilemap, searches for a blank “ruletile” and does that logic to change the tiles to match that pattern. You will probably have issues coding the ends of a platform, but it’s something you could do.

    Method3: What the above poster said, with extend neighbor, may also work.
     
  4. beethanmaxwell

    beethanmaxwell

    Joined:
    Oct 15, 2023
    Posts:
    3
    Many many thanks for the extended explanation!

    My primary use-case is the ability to "paint" long multiple platforms so it's way easier to draw a straight line of 40 blocks rather then place by hand 10 GameObjects which are 4-tiles wide.
    Despite my extensive background in OOP and IT im' not sure if for my current purposes it's be faster to position by hand or to create the relevant code (I don't have any Unity know as of today :p).
    My current bet is to postpone this to a later stage when I better understand the Unity ecosystem and can reliably estimate the effort :)
     
  5. beethanmaxwell

    beethanmaxwell

    Joined:
    Oct 15, 2023
    Posts:
    3
    Many many thanks for the detailed instructions when I'll get back to it I'll start from there.

    I'm very surprised thou to learn that there's no such thing already available in the marketplace!