Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Tilemap and colliders

Discussion in '2D' started by unity_0Z3t5xHxnMCdOg, Jul 12, 2019.

  1. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    Using Unity 2019.1.8

    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?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.
     
  3. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    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.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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).
     
  5. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    Yes that's exactly what i mean. I uploaded 2 screenshots before and after checking "UsedByComposite".
     

    Attached Files:

  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.
     
  7. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    Do you want me to upload the project so you can have a look? I don't really mind.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.
     
  9. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    Ok so that is my problem.

    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.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.
     
  11. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    They don't have a rigidbody.

    The first three screenshots are the problematic part, the last two are what i expect to see happening.
     

    Attached Files:

  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.
     
  13. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,785
    Sorry about that but children of a tilemap cannot use the composite collider of the parent tilemap; at least they don't in my current project.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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:
     
  15. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,785
    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.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    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.

    https://gyazo.com/19c8e2d46366c08e7caa503998900441

    Here's the same thing but the child as a prefab (which won't make any difference):

    https://gyazo.com/e23eca880645602e797e7f28a77b98ae
     
    Last edited: Jul 12, 2019
  17. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    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!
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    Shouldn't make a difference and in my second link I use prefabs too. Boxes also work.

    Please feel free to send me your project though. There's no need to find another way to do it.
     
  19. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    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.
    https://drive.google.com/open?id=1kb9FhWwO3NbFDtnw_2Xu2yG3jcwxE6NO

    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.
     
    Last edited: Jul 13, 2019
  20. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,785
    You just need to set "Extrude Edges" to 0 in your sprites import settings. :)
     
    unity_0Z3t5xHxnMCdOg likes this.
  21. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    Oh i didn't know about that thanks! This works too!
     
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    I would suggest setting you CompositeCollider2D property "Offset Distance" to at least "0.001". Note that you may have to disable/enable the GO with the CompositeCollider2D on it for this to take effect (this is a bug and I've mentioned it to the original dev of that property).

    This doesn't relate to tilemaps or sprites. If you create a GO with a composite and add two child GO with BoxCollider2D set to use the composite then they are not being merged in 2019.1.10f1 which seems to be related to the default "Offset Distance" property which is very, very small. Setting this to at least "0.001" solves this case where vertex are almost exactly overlapping. This value should probably be the default and certainly the composite should be recreated when the property is changed.

    https://gyazo.com/b824c7c020024d0550f846e14c20f200

    Hope this helps.
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    3,063
    Just an update to say that I was just informed that there was a backport fix that apparently landed in 2019.1.11 (not released yet) where the composite is updated when the property is changed.
     
    unity_0Z3t5xHxnMCdOg likes this.
  24. unity_0Z3t5xHxnMCdOg

    unity_0Z3t5xHxnMCdOg

    Joined:
    Jul 11, 2019
    Posts:
    10
    I believe i played with that value a bit but as i saw no difference i quickly reverted it back to the default value. Good to know it's fixed now! Thanks for the help, you are awesome!
     
  25. aqsanadeem82

    aqsanadeem82

    Joined:
    Sep 15, 2018
    Posts:
    23
    Chaning "Offset Distance" to at least "0.001" worked for me.
     
  26. howler123

    howler123

    Joined:
    May 7, 2017
    Posts:
    11
    Okay, I know what your problem is. You need to setup tilemap child for the area that the player walks on. You will need to add a TIlemapCollider2d and check "Used by composite" and then add a composite collider 2d. You will get a Rigidbody2d auto-added due to the composite. Set the rigidbody2d to kinematic and make sure and check the freeze rotation on the z-axis. This will smooth the floor up so your player will not get stuck.
     
unityunity