Search Unity

Question Best way to get desired colliders on tilesheet?

Discussion in '2D' started by PaperMouseGames, Jun 18, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there, so I'm working with tilemaps and I'm still very much new to this, but I'm having trouble with a few aspects.

    The main issue I'm struggling with right now is that I want my tiles (16x16 pixel sprites) to have colliders on them, but I don't want all the colliders to be 16x16 box colliders which is the only thing I can really generate by default.

    I know how to edit a collider for a given sprite manually, and I've been doing this, but as my tilesheet grows this is getting really tedious, especially since when I have a new version of the tilesheet I have to redo all the ones I had before.

    Is there any strategy to doing this for games that have a lot of different tiles?

    Right now I'm using a single tilesheet for all my tiles, but I'm debating making multiple tilesheets for each tile shape, so that all the colliders on a given sheet are the same, but I would appreciate any advice on this so I don't have to constantly be setting the colliders for dozens of tiles manually.

    EDIT: I just realized my idea of putting different collider shaped tiles on a different tilesheet won't work because I'm trying to change the shape of the collider so it's not what is automatically generated by the sprite editor and I can't find any way to do that other than manually tile by tile.
     
    Last edited: Jun 18, 2020
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I'm not sure the best method, but the way I'm doing it is to have 2 tilemaps, 1 which is strictly colliders, and 1 which is what the player sees. The collider map obvious is the only one with the tilemap collider and it just simple pink shapes that designate the colliders so I can easily inspect them visually just by shutting off the visual tilemap. Like I said, probably not the best way to go about it, but works for me so far. Of course, one advantage I have is my map is generated in script so I can easily get the two tilemaps to properly sync up. Manually having to do this I'm sure is much more a pain.
     
    PaperMouseGames likes this.
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Thanks for the reply, this sounds like a great idea! I'm not entirely sure how you've set it up so could I ask you to elaborate on this a bit more? Or if you used a tutorial / guide maybe a link?

    I don't know of a way to switch between the two images when I'm editing the sprites but I've only ever done that through the inspector, never even thought to do it through a script.
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    create two tilemaps, one set to sort below the other (changing the z coordinate of the tilemap does the job). The one that is below is the one that the player should never see and it is the one with the tilemap collider. Again, for me, I populate both using code so I just have a scriptable object that pairs up the colliders version of the tile graphic with the visible version of the tile graphic and it just goes through and assigned to each tilemap appropriately. If you're manually painting you'd probably have to switch which tilemap is in front when you go to paint the collider tilemap so you can see what you're giving collision to. And, again, the collider tiles are kept super simple, just pink shapes that match up to what you want the collision to be. For instance, for me, I'm doing isometric so I want the collision to be at the base of the graphic rather than match it, so I have the tile graphic (which looks like a cube) and the collider graphic (which is just a pink diamond at the base of the same area).

    My particular game has to dynamically change the colliders all the time as the player moves up and down the vertical space of the game.
     
    PaperMouseGames likes this.
  5. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    I see, so if I wanted to do it with manual painting I would need to paint the while map twice? Once for the sprites, and once for the collider map? That's not great in terms of convenience, though it might be preferable to changing each individual tile's collider.

    It's tough, because you can automate the sprite cutting of a sprite sheet by cell size, but I don't think there's any way (using the default sprite sheet editor) to automate the colliders of all the sprites on a sheet.

    I guess the ideal thing would be to write up a script that can be run in the editor that creates the colliders for each sprite in a sprite sheet. Not sure that's even possible in Unity though.
     
  6. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Well, if you always have a X tile = Y collider type setup, you can auto populate the collider map with a script, you just have to have some data somewhere to associate the tiles with their corresponding collider. You then have your script examine each tile in the visible map and populate the collider map with the corresponding collider tile. If it is a manually painted you can have to fire up on startup of the tilemap since you don't have to change it constantly like I do (I'm doing an isometric game where you can go up and down platforms so the colliders change as the player's Z changes).
     
  7. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    This is super interesting, thanks! I've never done anything with tiles or tilemaps via script so I'll have to do some research for the functions and whatnot but this sounds like it would be a fairly good way of doing things. Thanks again!