Search Unity

Resolved Procedurally Generated Tilemap Adjustments

Discussion in '2D' started by lack_05, May 24, 2020.

  1. lack_05

    lack_05

    Joined:
    Feb 20, 2019
    Posts:
    20
    So I have a tile spawner that spawns tiles randomly but they just look like squares is there any way I can add corners to them and make them smoothed out rather than just being squares here's my code for placing the tiles. (Some tiles randomly spawn in the center too if anyone code help me on that).
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEditor;
    6.  
    7. public class TileAutomata : MonoBehaviour
    8. {
    9.     [Range(0, 100)]
    10.     public int iniChance;
    11.     [Range(1, 8)]
    12.     public int birthLimit;
    13.     [Range(1, 8)]
    14.     public int deathLimit;
    15.  
    16.     [Range(1, 10)]
    17.     public int numR;
    18.     private int count = 0;
    19.  
    20.     private int[,] terrainMap;
    21.     public Vector3Int tmpSize;
    22.     public Tilemap topMap;
    23.     public Tilemap botMap;
    24.     public TerrainTile topTile;
    25.     public AnimatedTile botTile;
    26.     public bool usesBottomTile;
    27.  
    28.     int width;
    29.     int height;
    30.  
    31.     private void Start()
    32.     {
    33.         doSim(numR);
    34.     }
    35.  
    36.     public void doSim(int nu)
    37.     {
    38.         clearMap(false);
    39.         width = tmpSize.x;
    40.         height = tmpSize.y;
    41.  
    42.         if (terrainMap == null)
    43.         {
    44.             terrainMap = new int[width, height];
    45.             initPos();
    46.         }
    47.  
    48.         for (int i = 0; i < nu; i++)
    49.         {
    50.             terrainMap = genTilePos(terrainMap);
    51.         }
    52.  
    53.         for (int x = 0; x < width; x++)
    54.         {
    55.             for (int y = 0; y < height; y++)
    56.             {
    57.                 if (terrainMap[x, y] == 1)
    58.                     topMap.SetTile(new Vector3Int(-x + width / 2, -y + height / 2, 0), topTile);
    59.                 if (usesBottomTile)
    60.                 {
    61.                     botMap.SetTile(new Vector3Int(-x + width / 2, -y + height / 2, 0), botTile);
    62.                 }
    63.             }
    64.         }
    65.     }
    66.  
    67.     public void initPos()
    68.     {
    69.         for (int x = 0; x < width; x++)
    70.         {
    71.             for (int y = 0; y < height; y++)
    72.             {
    73.                 terrainMap[x, y] = Random.Range(1, 101) < iniChance ? 1 : 0;
    74.             }
    75.         }
    76.     }
    77.  
    78.  
    79.     public int[,] genTilePos(int[,] oldMap)
    80.     {
    81.         int[,] newMap = new int[width, height];
    82.         int neighb;
    83.         BoundsInt myB = new BoundsInt(-1, -1, 0, 3, 3, 1);
    84.  
    85.         for (int x = 0; x < width; x++)
    86.         {
    87.             for (int y = 0; y < height; y++)
    88.             {
    89.                 neighb = 0;
    90.                 foreach (var b in myB.allPositionsWithin)
    91.                 {
    92.                     if (b.x == 0 && b.y == 0) continue;
    93.                     if (x + b.x >= 0 && x + b.x < width && y + b.y >= 0 && y + b.y < height)
    94.                     {
    95.                         neighb += oldMap[x + b.x, y + b.y];
    96.                     }
    97.                     else
    98.                     {
    99.                         neighb++;
    100.                     }
    101.                 }
    102.  
    103.                 if (oldMap[x, y] == 1)
    104.                 {
    105.                     if (neighb < deathLimit) newMap[x, y] = 0;
    106.  
    107.                     else
    108.                     {
    109.                         newMap[x, y] = 1;
    110.                     }
    111.                 }
    112.  
    113.                 if (oldMap[x, y] == 0)
    114.                 {
    115.                     if (neighb > birthLimit) newMap[x, y] = 1;
    116.  
    117.                     else
    118.                     {
    119.                         newMap[x, y] = 0;
    120.                     }
    121.                 }
    122.             }
    123.         }
    124.         return newMap;
    125.     }
    126.  
    127.     void Update()
    128.     {
    129.         if (Input.GetMouseButtonDown(1))
    130.         {
    131.             clearMap(true);
    132.         }
    133.         if (Input.GetMouseButtonDown(0))
    134.         {
    135.             clearMap(true);
    136.             doSim(numR);
    137.         }
    138.     }
    139.  
    140.     public void clearMap(bool complete)
    141.     {
    142.         topMap.ClearAllTiles();
    143.         botMap.ClearAllTiles();
    144.         if (complete)
    145.         {
    146.             terrainMap = null;
    147.         }
    148.     }
    149. }
    150.  
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    This could be from a couple things, but it's probably from your sprite asset needing a different Pivot Point in SpriteEditor. It could also be from manipulating its transform Matrix4x4 from the Tile's script, if custom, while having a non-mirrorable pivot point.

    It's hard to know what look you're going for from the description. Depending on what you want, it might take a shader to fix, it might take changing your artwork and incorporating RuleTiles. The latter might be easier if shaders are unfamiliar. What RuleTiles do is they allow the tile to select a different sprite depending on what other tiles surround them, so it would be possible to "de-square" your tiles by having different sprites your RuleTile knows to select. I forget how TerrainTiles work - they might be sufficient and work similarly, if you supply the different sprites for them.
     
  3. lack_05

    lack_05

    Joined:
    Feb 20, 2019
    Posts:
    20
    Hi thanks for the response! I'll post a screenshot of what I mean to clarify. The tile is placed but the sprite shows up as a square. Is there anyway I could alter the code to make it so these tiles would be destroyed or something?
     

    Attached Files:

  4. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    looks like a problem in your rule tiles, its not tiling correctly on diagonals and some other situations
     
    eses likes this.
  5. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Looking at it it seems like you have a special case where two corners are the same terrain and the two other are not, are you checking for it and placing the appropiate tile, do you have the single tile for two corners?

    (edit)
    Looking at it I take it that terrain tile decide what type of tile it is supposed to be when placed and it seems the issue can be it selects the wrong tile, can we see the terrain tile script?
     
    Last edited: May 25, 2020