I’m wanting to get the Collider Type (None, Sprite, Grid) for a specific TileBase which i got via a tilemap
Although looking at the docs for TileBase there doesn’t appear to be any variable which contain info on the collider type the only thing that sounds like it would be useful is .GetTileData but looking at the docs it appears that this doesn’t contain any info on the collider either, I’ve tried researching for the solution but have found nothing in regards to this.
It would be greatly appreciated if anyone could let me know how i can get the collider type of a specific TileBase tile as this is a problem i have been stuck with for a while now.
I think it’s in a component on the GameObject, I believe this one:
Caveat: I’ve not used that, and a TilemapCollider2D is a Component (i.e., can only live on a GameObject) whereas a TileBase is actually a ScriptableObject. Maybe the TileBase gives the shape info to the collider? Now I’m just guessing because I don’t have a handy setup to mess with it just now.
So basically i’m wanting to check the collider type of an individual tile to check if its solid or not (None or Sprite/Grid) i just checked TilemapCollider2D and i don’t think it has a way to check the type of individual tiles on the tilemap correct me if i’m wrong.
I’m sorry, this has gone beyond my knowledge of tilemaps… I know what they are (been using them since 1983 on the Vic20) but I have not used the Unity implementation of them.
I do know that normally when you check “type of collider,” it’s either the C# type, or else you use a PhysicMaterial to change and detect the qualities.
If you want to embed more info in a traditional Unity collider you make a script and co-attach it and put whatever you want in that. I do that to make a floor deadly for instance.
Ok so just to elaborate on what im wanting to achieve:
I want to get a boolean array of all tiles on the tilemap for if they’re solid or not None being False and Grid/Sprite being True.
What i have currently tried is to get the tilemap and loop through all tiles:
for (int x = 0; x < tilemapBounds.size.x; x++)
{
for (int y = 0; y < tilemapBounds.size.y; y++)
{
TileBase tile = allTiles[x + y * tilemapBounds.size.x];
if (tile != null)
{
//get tile collider type
}
}
}
Although i have found no solution for getting the collider type from an individual tile. Hopefully this should explain exactly what i’m trying to accomplish.
sorry to spam my own thread but i have found a struct which contains what i’m looking for which might be helpful for anyone wanting to help find the solution which is TileData:
The problem is though i have no idea how to get this TileData struct, at first glance it looks as if GetTileData would accomplish this but further looking into it GetTileData appears to be a override method you use for implementing custom tiles and not used to get the TileData by looking at the docs.
So i guess my question changes to: how can i get TileData from a TileBase?
BoundsInt tilemapBounds = tilemap.cellBounds;
for (int x = 0; x < tilemapBounds.size.x; x++)
{
for (int y = 0; y < tilemapBounds.size.y; y++)
{
UnityEngine.Tilemaps.Tile t = tilemap.GetTile<UnityEngine.Tilemaps.Tile>(new Vector3Int(x, y, 0));
if (t != null)
{
print(t.name + ", " + t.colliderType);
}
}
}
its not TileBase which contains the ColliderType but instead Tile i simply had to get that instead and just got the colliderType variable out of that.
Thanks for those who responded and helped out.