One Composite Collider 2D. It holds 1 breakable block tilemap, 1 terrain tilemap, and platform GameObjects. In the picture below, there are 2 tilemaps (terrain and breakable block) and 2 game objects (the two platforms).
Whenever the player is in state X, the breakable block tilemap is taken out of the composite collider 2D by unchecking used by composite. This can cause issues if there are a lot of tile.
When the player switched to state O the breakable block tilemap is put back into the composite collider 2D.
As you can see from the above image, you can switch between these two states fast in the game. Is there anyway to optimize this?
This can be especially bad when you are breaking blocks and it has to rebuild every block you break. Meaning, every time you break a block, the composite collider 2D is rebuilt.
I know I can set it to manual and rebuild it once all the blocks are broken in that frame, but it still doesn’t address the issue of how long it takes to rebuild. Is there anyway to optimize this?
EDIT: Another issue that doesn’t seem to even have a way to optimize is those platforms connected to the terrain. They have to uncheck used by composite when the player is under them since they have the platform effector 2D on them and it won’t work, of course, in a composite collider 2D.
The reason this all has to be setup this way is to fix ghost collisions.
Hi, do you get a lot of ghost collisions when you separate the breakable block tilemap from the terrain tilemap and the platform objects? Having all of these together in one CompositeCollider2D will not be great as all the sub-objects need to be composited again.
Also, could you share how you are breaking the blocks? (eg. calling Tilemap.SetTile(position, null) for each tile or otherwise). Thanks!
I have gotten ghost collisions between terrain tilemap and platform objects before combining them. I haven’t gotten between breakable blocks and terrain tilemap but I haven’t tested going over their seams as much. I know that these ghost collisions can just suddenly appear and I am too afraid of releasing a production game with the chance of that happening.
I am using SetTile as I am only breaking like max 4-6 blocks in a frame. Would using SetTiles be much more beneficial by gathering all blocks and breaking them at the end of the frame using that method?
Question: Is there no way to optimize this further? Even if I wanted to risk it and detach the two tilemaps, the platforms have to be under the terrain tilemap and they will cause the composite collider 2D to be rebuild since I need the platform effector 2D to work when the player is under them (they are single way platforms so you have to be able to jump through them). I don’t see anyway around this?
Unfortunately, the compositing is the function that is taking up the most time, rather than SetTiles itself. We will work on improving this!
Ideally, the “breakable” parts of your level should not be composited with the static parts of your level, although this may cause ghost collisions to occur. This would bring the most performance improvement though.
There are two properties you could set which could help with performance: Extrusion Factor on TilemapCollider2D and Offset Distance on CompositeCollider2D. Setting both to 0 would improve the compositing process by reducing the work composite micro-gaps between Tiles. However, this could introduce micro-gaps in the collider with the Tiles are not aligned well, eg. physics shapes for Tiles are at 0.9999 for an edge of a cell, rather than at 1.0. This can be visually inspected to see if there are issues.
For Tiles themselves, you may want to consider setting the Collider Type to Grid where it makes sense for the Tile, rather than use Sprite as the Collider Type. This depends on the art of your Tiles though. Simpler physics shapes make for faster compositing!
I am not sure what you mean for the Platform Effector 2D. Are both the terrain and the platforms one way platforms?
I had the offset distance set to 0, but not the extrusion factor. I set that to 0, thanks! (Fun fact: Offset Distance not being set to 0 causes ghost collisions when falling off the edge of the collider.)
A majority of the tiles are set to grid
Sorry, the platforms have the effector, not the terrain. If the platforms are set to Use By Composite the platform effectors won’t work.
Just curious when you think this would be implemented? I am launching on mobile (that is why this is a main concern) and near the middle of next year if everything goes according to plan. Is there anyway to help with this such as submitting a bug report with the level map I have?
The Platform Effector will need to be used by the currently active Collider2D for it to work. This would mean that the Platform Effector would need to be attached to the CompositeCollider2D with the Terrain and Platforms. That would also mean the composited Terrain would have the Platform Effector effect with the Platforms.
Yes, that would be helpful! Remember to post the case number here, although responses may be delayed due to the new bug reporting backend. If you can share details, such as the Unity version you are targeting and the details of the mobile devices you are targeting (min spec, average spec etc), that would be great!
I know its not really the best, but I can’t really give an exact min/average spec of mobile device I am currently targeting. There is a lot of optimizations with UIToolkit right now that I am waiting for the developers to fix before I can really figure out what phones I can target. Even on a iPhone XS, UIToolkit has issues so its hard for me to know which phones I can target until that is sorted out.