Search Unity

Different Collision Layers for different Tile in the new Tilemap system

Discussion in '2D' started by Le_Tai, Nov 20, 2017.

Thread Status:
Not open for further replies.
  1. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    442
    I'm making a platformer with water and land. I need them to belong to different collision layer so that the player controller can interact with them differently. Is this possible with the new system?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You could have two tilemaps, each with a different tilemap collider and layer.
     
  3. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    442
    That what I'm doing, but it just feels wrong. There should be a better way of doing this
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Why does it feel wrong? That is the intended workflow. You paint the tiles in layered tilemaps, each with their own collision shapes, all of them children of the same grid object. The palette window allows you to switch between brushes, tiles, and tilemaps on the fly, and you paint directly in the scene.
     
  5. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    442
    I think it would be better if we can set layer for each tile.

    When using multiple tilemap, it is not apparent which map is being selected, if you draw on the wrong map you might not know it.

    Also, different tilemap can overlap, if you accidentally draw on top of another tilemap, it will lead to wrong behavior at runtime that isn't easy to tell. For example, in my case, if my water tile isn't transparent - just a blue square - and I draw it on top of a ground tile, at runtime the character might walk on top of the water and I will think there something wrong with my code. As the project gets more complicated, finding the overlapped tile will become extremely frustating.

    Another problem is that rule-tile in different map can't interact with each other. What if you want the ground near water to turn in to sand automatically? This would be possible with the custom tile provided in 2d-extra repo, but only if the tiles belong to the same map.

    I'm only making a simple game, and I already run into problems with this kind of workflow; people who are making much bigger game might even have more problem with it.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I would expect that your ground/water/sand tiles would all be part of the same tilemap, and you would define the collision per-tile via the SpriteEditor collision shape definition. I would expect that the water would have collider shapes defined, but the ground and sand portions would not which would allow the player to walk over the ground and sand, but not the edge of the water.

    However if I'm not mistaken, you have access to the GameObject that is instantiated per-tile using the TileData struct when implementing your own Tile class. You could set the layer there perhaps by overriding the GetTileData function and setting the GameObject layer.

    https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles-TileData.html

    Possibly like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. [CreateAssetMenu]
    5. public class TileWithLayer : Tile {
    6.     public string layer;
    7.  
    8.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    9.     {
    10.         base.GetTileData(position, tilemap, ref tileData);
    11.         tileData.gameObject.layer = LayerMask.NameToLayer(layer);
    12.     }
    13. }
    14.  
    I haven't tested this, but it should be possible I think.
     
    Last edited: Nov 24, 2017
    CheekySparrow78 likes this.
  7. jimfhurley

    jimfhurley

    Joined:
    Mar 6, 2015
    Posts:
    7
    It is easy to see if you have things on the wrong tilemap by deactivating the tilemap, then looking to see if those tiles are still visible. If they are, then they aren't in the proper tilemap. E.g., deactivating the water tile map then looking to see if any water tiles remain.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Please look at the dates of threads otherwise you end up replying and necroing a thread from nearly 5 years ago.
     
    MousePods and eses like this.
Thread Status:
Not open for further replies.