I found nothing on the internet so i’m asking here:
I’m making a 2d retro style platformer using unity’s tilemap system. I have a prefab brush to paint my prefabs. Painted prefabs are under a parent tilemap. I’m trying to add a composite collider2d but it doesn’t work which results in a known problem, random collisions caused by two adjacent colliders in the tilemap. Please note that if possible i want player’s collider to be a box2d and NOT a mix of box2d for the body and circle for the feet because i’m using raycasts and i’m still unfamiliar with them.
Is there any solution or workaround to this problem?
When trying to get help, it’s best to elaborate on what problem you’re actually having rather than just stating it doesn’t work.
Could you explain what isn’t working exactly? If you add a PolygonCollider2D and BoxCollider2D on a child GameObject of the CompositeCollider2D then check its “UsedByComposite” checkbox then it’ll be used by the composite. The TilemapCollider2D isn’t special, it does the same so if that works there then there’s no reason why other colliders won’t work either.
Ok let me explain. I have BoxCollider2D attached to every child GameObject, painting them with the prefab brush into the scene and they all have their own box collider. What i’m trying to do is check the “UsedByComposite” checkbox to create one collider for all of them as expected. But when i check the checkbox the prefabs still have their own individual box colliders. What i’m asking is if it’s possible to create one uniform collider for all of them in order to eliminate the issue i mentioned above or if there is a workaround to this.
Could you explain what you mean by this? Are you saying that the box does not get blended in the parent CompositeCollider2D? If you look at the “Info” roll-out on the collider you should see that when you check the “UsedByComposite” that the shape count goes from 1 to 0 (for the case of a BoxCollider2D).
The screenshots only show you selecting the tilemap to use the composite so I don’t understand how that shows anything.
If you’re saying the child prefab instances when you select used-by-composite are not being blended into the composite I would ask how do you know this? What are you expecting to see and can you show that.
Not really. Please explain what you are expecting in detail and show it not working. Having the project won’t help if I don’t know what your expectations are and what you are seeing going wrong.
The player is getting stuck at random spots while walking on them. I know the reason behind this is because colliders seem to be perfectly placed but in reality they are not. This is not happening when the player walks on the floor tilemap which has “UsedByComposite” checked, all the colliders are blended and it is working as expected( Note that the floor tilemap doesn’t contain prefabs, only tiles from the tile pallete).
Also the gizmos are not changing like they did when i created the tilemap for the floor (the green line which appears when you check the “UsedByComposite” and encloses adjacent Gameobjects making their individual colliders one big collider).
I want to know if there is any way to blend the prefabs’ colliders or if i have to resolve collisions with code.
As I said above, can you check to see if the shape-count goes to zero on the colliders you’re talking about when you do this? Maybe even show me a screenshot of the gizmos and what you’re expecting to see.
being a prefab makes no difference, the composite will blend child colliders full-stop. Just make sure those colliders don’t have a rigidbody on them as they are attached to that body and not the composite one.
Just trying doing this on a fresh project, it should work fine.
Sorry but those screenshots just add more questions. I’m still waiting for you to confirm that the shape count goes to zero on these prefabs. I wanted to see the gizmos of the prefab instances so is that shown in shot#1 or is that the gizmo from the tilemap collider?
Honestly, this isn’t useful so go ahead upload a simple repro project and I’ll take a look. In the meantime, please confirm that it is those “brickblocks” you’re having problem with (you have not even confirmed that) or maybe just isolate those and show them configued to use the composite and not using the composite and the gizmos. Please remove the tilemapcollider if that has tiles on it.
To me it just looks like you’ve got a tilemapcollider that uses the composite and then child boxes that are not using the composite.
The TilemapCollider2D has nothing whatsoever to do with a CompositeCollider2D and they don’t control what it can or cannot compose.
The CompositeCollider2D can compose any collider with the UsedByComposite on it that is on the same GameObject or children. This includes the BoxCollider2D, PolygonCollider2D and TilemapCollider2D.
As an example, here’s one of my original dev videos that shows that:
Are we talking about the same thing? I have a tilemap that has a tilemap collider and a composite collider. This tilemap has children game objects that have a Collider2D attached to them. If I check “use composite” on these children, Unity will ask for a composite collider but it won’t use the composite collider that is used by the tilemap.
That is what is shown above in the video. The TilemapCollider2D doesn’t change how the CompositeCollider2D works, it’s just another collider.
It asks? When you check that option, it’ll look for a CompositeCollider2D on the same GameObject or its parent only looking until it finds either the transform root or a Rigidbody2D. If it doesn’t find one it’ll show a warning underneath the Collider2D in question.
Just to make sure someone didn’t break something I just added a Tilemap, painting a few tiles, added a composite and set the tilemap collider to use it then added a child polygon collider and set that and it works fine.
Here’s the same thing but the child as a prefab (which won’t make any difference):
My project has exactly the same setup as the one you are showing in the video, but i have prefabs instead of GameObjects and and BoxCollider2D instead of polygon. Then i check the UsedByComposite like you did in the video but it doesn’t blend. Anyway i’ll think of another way to do it. Thanks for your time!
After hours of testing and trying various things and suggestions i found what the problem was. For some reason painting with brush or drag&drop under the tilemap(prefab or not) it seems that there is a gap between adjacent tiles even if we can’t see it. If i move one tile even the slightest distance towards the one next to it the colliders are getting blended. Is that a bug?
Here is the project i used for testing. You can try it for yourself. Paint or drag&drop 2 GameObjects in adjacent cells and notice that they have individual colliders. Then try moving one towards the other and their colliders will get blended.
For anyone interested i found a workaround too. I changed the grid cell size to a number very close to 1 but not 1, something like 0.9999999.