Composite Collider 2D Doesn’t Generate Correct Geometry

Hi,

I updated to 2022.3.13f1 from 2022.1 beta and now when I use SetTile(position, null), and then call compositeCollider2D.GenerateGeometry(), it will not remove those old tiles.

NOTE: I have the composite collider set to manual when I call GenerateGeometry method. Then I set it back to synchronous.

Was there any major updates to the composite collider that would stop this from working?

here is a video:

e1cxhd

You can see when I go to triangle, the breakable blocks tilemap will correctly update its collider. Its useByComposite is false.

Then I turn back to circle. I call GenerateGeometry on the Terrain Tilemap’s composite collider. However, you can see that it didn’t remove those tiles that were set to null.

Actually, the video is really confusing and not clear at all so I’m not following what it’s showing. Too much cursor waving and flicking between things.

Note: I did report it, IN-61690

Let me try again :smile:

In the video, you can see two tilemaps.

Terrain Tilemap (parent) and Breakable blocks (child). The parent has a composite collider 2D and the child is set to useByComposite.

  • You are a circle. You can see the breakable blocks lined up to the right side of the circle. I turn off the breakable block tilemap renderer and show the collision
  • I turn to a triangle. This breaks some of those blocks using SetTile(position, null). You can see that they are gone from the collision.
  • I turn back to circle. You can see useByComposite is rechecked on the breakable blocks So this means that the parent tilemap which has the composite collider 2D should rebuild its collision and not have those 2 tiles.
  • I then show the terrain tilemap. Those 2 blocks that were set to null are still in the composite collider 2D. This only stopped working when I updated from 2022.1 to 2022.3

h9128m

I’ll have to take your word for it about those physics shapes because the low-res video isn’t showing the shape outlines very well at all and I only occasionally see some faint bits of green lines. I see the cursor pointing and things changing in the inspector but cannot see much in the scene which is the most important thing to see.

All I can see are a few faint lines about 20 seconds in. :frowning: Do you know there’s a collider fill mode for gizmos? They would be very clear then in any resolution.

I presume if you take off the Composite then you see the physics shapes?

Regardless, I’m sure the Tilemap dev will investigate here though given your report.

Nope. There’s been a lot of tilemap changes though.

The composite being enabled on a collider is nothing to do with the composite, the control is entirely up to the collider in question to get the work done by adding/removing things to/from the composite. This means, if the above is not working then it’s the TilemapCollider2D that is at fault here, not the CompositeCollider2D. This is why I suggest this is something the Tilemap dev can/will look at.

1 Like

Ah okay! Thanks.

I did. That would have been better. Thanks for the tip!

1 Like

Just for anyone looking for a solution if they run into this.

There is a new variable called m_MaximumTileChangeCount. For my case I needed to set it to 0 so calling GenerateGeometry() worked.

I believe that was added all the way back in 2019.1 if you’re referring to this: Unity - Scripting API: Tilemaps.TilemapCollider2D.maximumTileChangeCount

You’d be better off leaving that alone and calling: Unity - Scripting API: Tilemaps.TilemapCollider2D.ProcessTilemapChanges

Doing each tile change immediately is expensive. Doing them all in a queue then processing in one go would be much faster, especially if you’re also using the CompositeCollider2D.

Thanks for the info! You’re right, that isn’t a new variable.

Using ProcessTilemapChanges doesn’t work either. The only way I can get it to work is to set maximumTileChangeCount to 0. Something seemed to change on the backend between 2022.1 and 2022.3.

I don’t think there is any performance drops because I do set the compositeCollider2D to manual generation instead of synchronous. Then I can GenerateGeometry when optimal.

1 Like

Sounds like a bug to me. I’m not the dev on that but I can check the code in 2022.3 to see what it does … hold please. :wink:

1 Like

The code looks fine to me and does indeed process the tile changes which are queued. If the number of changes exceeds the maximum queue size then it simply recreates the whole collider for the same effect.

I just did a quick test and it seems to work too so not sure what’s going on there for you.

Yeah…I don’t know. I know 100% my code worked in the older version of Unity and when upgrading without any code changes, it stopped.

It might be due to the fact I am settings useByComposite off and then turning it back on or any other number of factors.

Thanks for looking into it. At least I have a workaround.