Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do I customize my tilemap collisions?

Discussion in '2D' started by Marscaleb, Apr 24, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    The Tilemap collider component I added to my tilemap is generating all kinds of odd shapes that are causing problems with my character collision.



    I can very easily create the collision I want for each tile with a simple box collider, but I don't see how I can apply that collision to specific tiles in my tilemap.
    I could even make do with assigning a custom collider to every tile on my map, and then using different maps for the different collision shapes I need. But I can't even see how to do that.

    How do I set specific collisions for my tiles?
     
    Last edited: Jan 1, 2021
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    That doesn't seem to apply to tilemaps.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Have you tried in your tile script settings ColliderType to Grid?
    tileData.colliderType = Tile.ColliderType.Grid;

    So, you could have a custom tile that you paint grid-collider to sections of your tilemap.

    You may also want a Composite Collider 2D.
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    That is an improvement; now I can have a pure square instead of whatever wonky shape it tries to fit around my image.
    But is there any way to create custom colliders? Like if I wanted to use a box collider with specific dimensions, or draw my own polygon collider?
     
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    What happens with you use ColliderType.Sprite in combination with a Composite Collider? It should combine your custom-defined SpriteOutlines (defined w/in SpriteEditor) into combined polygons.
     
  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The Custom Physics Shape of a Sprite applies to the TilemapCollider2D if the Tile has the same Sprite and is using the ColliderType.Sprite. This allows you to set a specific Physics Shape for a particular Sprite.
     
  8. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    I know this thread is done at this point, but I thought I should mention this on behalf of anyone who comes across this thread in the future, facing the same problem.

    I didn't realize this was a thing, so I'm going to go over this very explicitly for anyone else who doesn't know about it.

    If you go into the Sprite Editor (select the actual image containing your tile, and there will be a "sprite editor" button in the inspector,) in the upper-left corner there is a drop-down box that says "Sprite editor," if you change that to "custom physics shape" then the editor window will let you create any polygon shape you want for an individual tile. (You have to select a tile and click "generate" before there is anything the edit, though.) And it has a VERY nice snapping option that makes it super-easy to make perfectly flat and uniform shapes.
    This will be the shape a tile uses if you set the tile to "sprite," which it does by default.

    I got lost originally because I was trying to edit the settings of the "tile" data that gets generated when you add a tile to a tilemap palette. I had no idea that I needed to adjust this through the sprite editor.

    This is actually the very first thing that was suggested in this thread, but I didn't realize that it impacted the shape tiles would use in the tilemap.
     
    Last edited: Jun 17, 2020
  9. gopacco

    gopacco

    Joined:
    Jan 2, 2020
    Posts:
    3
    Thank you Marscaleb, your last awnser is exactly what i was looking for.
     
  10. HolyBusiness

    HolyBusiness

    Joined:
    Mar 12, 2022
    Posts:
    1
    Awesome, that helped a ton! Thank you for going back to clarify. I'm new, but I will add this to my practice when solving problems.
     
    Last edited: Mar 12, 2022
  11. porygon

    porygon

    Joined:
    Dec 28, 2013
    Posts:
    4
    That helps a lot! Thank you.