Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Use an array to set a tilemap collider2D

Discussion in 'Scripting' started by Saw9yer, Nov 6, 2021.

  1. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    Hello everyone,

    I created a map using cellular automata.
    I display my map on a sprite.

    Now i want my player to interact with the map.
    My question is how to set my Tilemap collider2d based on my texture2D?

    This is my code, as you can see i just display a tilemap based on my texture (is just to see if it works, after i don't want to display my tilemap, i just want to use it to have a collision system) :
    Code (CSharp):
    1. void Start()
    2.     {
    3.         GameObject obj = GameObject.Find("Texture");
    4.         tilemap = GetComponent<Tilemap>();
    5.  
    6.         width = obj.GetComponent<CellularAutomataMap>().width;
    7.         height = obj.GetComponent<CellularAutomataMap>().height;
    8.  
    9.  
    10.         tilemap.ClearAllTiles();
    11.  
    12.         for (int x = 0; x < width; x++)
    13.         {
    14.             for (int y = 0; y < height; y++)
    15.             {
    16.                 if (obj.GetComponent<CellularAutomataMap>().cellMap[x, y] == true)
    17.                 {
    18.                     tilemap.SetTile(new Vector3Int(x, y, 0), tileExist);
    19.                 }
    20.             }
    21.         }
    22.     }  
    The final aim for me is to have a interactible map, i want to dig is the wall for exemple.
    Can you help me?
     
  2. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    I tried to simply add a tilemapCollider2D in the object that contain my script but nothing happen