Set tile / setTiles / SetTilesBlock lag (part 2?)

So some time ago i had an issue by setting several or even just 1 tile to null or to another new tile, that seems to be an issue with the composite collider recalculating the new shape on big tilemaps, so i just had to create several tilemaps and handle them separately, but sometimes (like in this situation) i NEED to keep them in the same tilemap, so i have a 32 x 440 tiles size tilemap (around 14k grid cells) but i just have like 1000 or less with a real tile on it, the rest is just null (nothing), so if i do “tilemap.SetTile(…, null)” the entire game freezes for 1-2 seconds while the composite collider seems to be working (attached image) taking all the CPU, i wonder if it’s a bettwe way to handle this because it’s frustrating, i just need the tilemap to work with “OnTriggerStay2D” and “OnTriggerExit2D” that’s why i added the tilemap a “Tilemap Collider 2D” (“used by composite” marked) and a “Composite Collider 2D” (with “is trigger” enabled), aswell as a “RigidBody 2D” (which is needed by the “Composite Collider 2D”) with body type “static”.

Edit: another idea is to create a tilemap for each area (small 3x3 areas), but that would create like… 100-150 tilemaps in runtime… don’t know if that could kill the cpu even more…

Thanks in advance!

If you set the Offset Distance property in the CompositeCollider2D to 0, do you see a difference in speed?

As mentioned before, providing more details would help us in handling this issue. Your use case with the CompositeCollider2D may be something we have not considered in terms of performance, and providing more information would help us fix this!

CompositeCollider2D isn’t based upon some chunked mesh building and was designed primarily for Static (non-changing) geometry although it was made fast so can be used in some dynamic scenarios. For it to successfully merge the geometry from a TilemapCollider2D it has to process all of it; there isn’t a shortcut so if you scale that up then the CPU time will scale too.

In the end, you might consider NOT using the CompositeCollider2D unless you have a very specific reason to do so. I know it’s used in outline mode to help reduce/remove ghost contacts but if you’re using it in polygon mode then you might get a reduction in memory usage but it might also be a pretty small reduction.

A good split would be to split between Tilemaps that won’t ever change and ones that will, assuming the ones that will can be kept relatively small.

1 Like

Well the min value is “5e-07” and that’s the default, also tried with other values from 0.001 to1, 10… nothing changes :frowning:

I need it for a “heat” area around a lava tile, like cubes, so the lava has 8 “heat” tiles around it when you break the lava i have to erase the heat tiles, so that’s why i need the composite collider to increase the heat for the player every second with OnTriggerStay2D

But what has any of that got to do with a CompositeCollider2D? Typically the primary reason it’s used on a TilemapCollider2D is to remove ghost collisions.

I need the collider set as trigger in order to increase the time within the tiles collider, if you have any better idea i’d love to know :slight_smile: (single colliders instead of composite makes the OnTriggerEnter/OnTriggerExit for every single tile so i lose the time when i need to keep track of the entire area)

This isn’t true. You get enter/exit for the whole TilemapCollider2D not per-tile. Each tile isn’t a separate collider.

All the composite does is merge geometry. If you apply this to a TilemapCollider2D you just get a different collider (CompositeCollider2D) but with merged geomety but still a single collider.

1 Like

Ah, I forgot to ask which version of the Unity Editor you are using. I guess the min value of 0 did not make it to that version of the Unity Editor. That would likely help with this situation, let me see what I can do about that!

1 Like

Well after watching some tutorials (like the Ruby’s one) all recommend to use a composite collider for tilemaps in order to increase performance, as i said when i remove the composite collider also have the problem that gogin from 1 tile to another even if they’re adjacent i get onexit/onenter so the “onstay” gets lost and the counter stats again, i fixed that issue by creating the physics layer manually in the sprite editor =D
So yes, your idea after a few tweaks worked! thanks! i’m just a bit worried about the performance, i suppose that having a lot of tiles without the composite takes more memory maybe? i means in the tutorials is recomended but well is working without lag in my side.

Currently using “2019.3.14f1” but i have this issue from like 3 versions ago :slight_smile:

The Unity Editor with the updated min value of 0 for the CompositeCollider2D should be available in 2019.4.1f1 soonish (I don’t actually know when). Do let us know if that will help with this issue!