Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tilemap Collider 2D At Runtime

Discussion in '2D Experimental Preview' started by NathanHold, Jan 28, 2017.

  1. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    The tilemap collider doesn't seem to work at runtime, only in the editor.

    As a workaround I set a GameObject on the all 'solid' tiles with a BoxCollider2D on it, because this works I assume this is a bug.

    The repro needs the following:
    • A player GO, it should have a RigidBody2D (Dynamic, Z rotation off), BoxCollider2D and a script which changes the velocity of the RigidBody to move it.
    • A TileMap with some 'Grid' collision tiles in it to block the player on the map
    From here the do a test in editor and a test in build. The build doesn't seem to work.

    Attached is a picture of my test where my player is able to go through the tile maps collision but not through the GameObject set for the tile:

    Test.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    What do you mean 'through the GameObject set for the tile"?

    You need to use the physics shape editor for the sprites you use otherwise it'll try to generate an outline from the sprite texture. If it has to generate an outline from the sprite texture, that texture has to be marked as read/write which means it'll be in memory which isn't good. If you don't generate a shape and the sprite texture isn't read/write then it'll fail to generate a shape. Therefore, set a physics shape in the editor for the sprite.
     
    Skitzofreni and TuckerFlynn like this.
  3. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    By 'through the GameObject set for the tile' I mean with this option set labelled GameObject. It has a BoxCollider2D which the player can't go through.

    TileData.PNG

    Originally I just set the Collider Type to Grid and it seems to create a collider fine:

    TileMapCollision.PNG

    Which is used correctly in editor, it's the build where the player goes right through it:

    EditorCollision.PNG

    Are you sure I should have to do that if I can see the Collider gizmo on the tilemap working fine?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    Right, so that's what you meant when you said
    Now I follow. So yes, you're not reliant upon the sprite and the TileMapCollider2D is hardwired (for the moment) to simply create a rectangle the size of each grid tile. You're saying this isn't working when you build a player; presumably this is preview#3?
     
  5. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    Yes, this is preview #3 and yes it isn't working for me when built to the windows player (I haven't tested others).
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    You'll need to produce some simple reproduction case because it works fine here. Either create a bug report or provide a project that I can access here so I can test it for you.
     
    tuquan likes this.
  7. nathan_epc

    nathan_epc

    Joined:
    Oct 8, 2015
    Posts:
    22
    Ah it looks like it does work.

    After looking at it some more, I think it was due to the colliders not updating if I change the type while not having scenes with that tile loaded. Is that expected workflow or is there going to be some 'pre-build' logic to rebuild colliders on scenes that haven't been touched since changing tile information?
     
  8. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    Apologies, the above is my post from my work account if that's confusing.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    I'm not sure I entirely follow what you're saying but I presume you mean changing the tile-asset then loading a scene and it changing with that physics change?

    Well know that if you're using the CompositeCollider2D, it works very hard to ensure that there's very little to do upon loading the scene. This means it doesn't recreate the colliders that use it. If that's a TileMapCollider2D then it won't be recreated due to some unknown tile colliderType change. We could have an option for that on the TileMapCollider2D that forces a recreation due to some speculative change but that would mean you get the hit of it being recreated and calculated when you load the scene. It certainly isn't something that's part of a normal workflow but would just work for all things apart from the TileMapCollider2D.

    If you're not using the CompositeCollider2D with the TileMapCollider2D then you'll need to explain in more detail what it is you're doing in terms of real Unity objects etc.

    This is part of the problem with Unity. It offers so much flexibility that it cannot do so without sacrifcing performance. If there's a fast path then it reduces flexibility.
     
  10. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    No I don't have a CompositeCollider2D.

    Okay a full explanation:

    I have a network lobby in one scene, this loads into the below scene when hosting or connected.

    I have a TileMap with a TileMapCollider component in the other scene.

    While editing 'tile' assets (In the project, in editor) I sometimes don't have the scene with the tile map in it loaded(That those tiles are in), this means that if I change a tiles collision type after already having placed it I have to load all scenes (in editor) that contain that tile map to allow that change to propagate.

    Is that the expected workflow? If so, is there work going into making that a bit easier?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    When you load the scene, the TileMapCollider2D (assuming then it isn't using a CompositeCollider2D) should just recreate itself from scratch i.e. it'll look at all the tiles in the associated TileMap upon load so any collider-type changes should be used. My point about the CompositeCollider2D was that this process doesn't happen when you use it to save the hit of having to create it from scratch when loading.

    In short, the TileMap doesn't store/serialize any collision geometry and neither does the TileMapCollider2D; it creates it when you load it (with the exception of when it's using a CompositeCollider2D as I stated above).

    So this sounds like a bug. There has been very little work on the TileMapCollider2D over the last few months however I'm scheduled to do some work on it ASAP to fix some known issues.
     
  12. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    When you say 'load the scene' are you referring to loading at run time or loading it in editor or both?

    I ask because I can make a change to a Tile in editor and then load a scene with that tile in it and the collision doesn't update for a good few minutes.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    They're the same thing.

    There's no way there can be a delay, there's no timer or anything.
     
  14. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    I still get very odd results for you saying loading a scene performs the update.

    I have noticed the following things to be more reliable in re-building the collider to match changes made to a tile compared to loading the scene.

    1. Ensuring the Palette window is open with a valid tile selected and putting the mouse into the scene window.
    2. Disabling\Enabling the Tile Map Collider.
     
  15. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I can also confirm that when you use it in run time to set the tiles using command such as :

    tilemap.SetTile(pos, tileBase);

    It doesn't update the collision reliably. It does sometimes and sometimes not. I am not sure when it does and when it doesn't. This isn't problem with delay or anything, it just does not rebuild the part of tilemap surrounding the position where the tile has been set.

    The only work around I found was to disable the TilemapCollider2D and enable it again to force it to rebuild entire colliders, not just around where it needs to, which is probably causing a lot of unnecessary overheads.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,266
    The TileMapCollider2D in the current release is very far behind the latest. Many of these update issues have gone away. We'll try to get an updated out ASAP.

    Thanks or the feedback.
     
    NathanHold likes this.
  17. japtar10101

    japtar10101

    Joined:
    Mar 13, 2010
    Posts:
    138
    I hate to revive an old thread, but I've bumped into a similar problem with a stable Unity 2017.2.0f3. Here's the source-code to my project:

    https://bitbucket.org/OmiyaGames/ludum-dare-40/src/dc38e66708f0c342b3ced5a10f5701f8cce115b4/?at=v0.1 (up to commit dc38e66 in "default" branch)

    Just like the previous posts, I create a new set of tilemaps on runtime (start), and it seems to work fine in editor. The moment I build, however, collision with ALL tilemap collider2D (even those I made no changes to) disappears.

    I do realize a few patches were released since, and I'll try testing them out soon. Will edit this post if anything changes.

    Edit: does not work with 2017.2.0p4, either.
    Edit 2: updated the URL above to tag where the bug was happening. I did create a work-around: re-create a box collider for every tile.
     
    Last edited: Dec 5, 2017
  18. Viseper

    Viseper

    Joined:
    Aug 22, 2019
    Posts:
    2
    I'm am using unity 2019.2.0f1 and I am having a similar bug so...
     
  19. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you file a bug with the Unity Bug Reporter with your project with the steps you used to reproduce this bug and post the case number here? That would be very helpful, thanks!