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:
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.
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.
Originally I just set the Collider Type to Grid and it seems to create a collider fine:
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?
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.
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?
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.
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?
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.
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.
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.
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.
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.
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!