Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 2D Levels Collisions, Need Advice

Discussion in 'Physics' started by ianian_, Aug 18, 2023.

  1. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    Hi everyone,

    I'm developing a 2D game using vector levels and I'm struggling with generating proper colliders. After a lot of googling I still can't find a solution for my case;

    • The levels are created in blender and are complex shapes. In the game they are completely static.
    • I'll be frequently modifying these levels in Blender and in future materials will be added in Blender.
    • Ideally, I'd like to edit the terrain in Blender, import it to Unity, and automatically have the updated level with colliders.
    • Here's an example super simple level consisting of two separate objects:

    upload_2023-8-18_7-48-38.png

    Approaches I've tried for enabling collisions with regular (box etc.) RigidBody2D objects:
    • Manually adding colliders - it works but since I'm editing the levels a lot it adds a lot of extra work, especially when the levels update during development.
    • Convert to sprite, then generate colliders out of it - again, seems like a lot of extra steps (export to image, import etc.) I do a lot of small adjustments, e.g moving a corner 5 or more times to make sure the player fits.
    • Generating polygon 2d collider with a script works, but it fails in about 10% of cases. I couldn't find anything in Asset Store that would "just work" for any 2d shape without worrying about vertices order etc. I'm more than happy to pay for a working solution.

    How would you implement this? The levels will be much larger than the example but low-poly so performance shouldn't become an issue.

    Thank you for your help!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,273
  3. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    I think it won't work in this case, from the docs it seems more suited to use for tiled sprites that are deformed.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,121
    Why would it fail? I don't know of any instances of it failing. Maybe you're just using it incorrectly?

    Note that there's also the CustomCollider2D where you can directly specify the physics shapes you'd like to use. This is also very fast.



     
  5. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    @MelvMay here's an example of it failing for me. The part in red rectangle doesn't collide with a box that properly collides with everything else in the scene. You can see the generated collider. For all other shapes in the scene it works, although during testing I had a few instances of this issue and I couldn't see any particular reason for this happening.

    upload_2023-8-18_13-38-1.png

    If I understand it correctly, CustomCollider2D is a structure to hold more primitive colliders but I'll still have to somehow process the polygons coming from Blender to generate them. Or does Unity provide some mechanism for doing this automatically?

    Thanks!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,121
    So you're providing the outline only correct as a path correct? Does the PolygonCollider2D component state it had to remove shapes because of colinear or vertices too close? It won't modify the data you pass unless there's a problem with it and it will always tell you if it does; you'll see a warning box appear in the component. Unfortnately it's not shown in the image above.

    Would it be possible to provide me with the scene with this polygon set-up so I can look at it for you or just provide me with the path points you use?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,121
    I don't know what "polygons" you have. If they're triangles then you can create polygon shapes as triangles if you wish; no more processing needed.
     
  8. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    Hi @MelvMay sure, I'll send you over the scene but please wait until Monday as I'm going to be offline over the weekend. Thanks!
     
  9. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    @MelvMay it turned out the objects coming from Blender had a few extra vertices that weren't connected to faces. Fixing the objects made the polygon collision generator work well. I suspect the earlier issues were also caused by me doing a terrible job in blender.

    This solves the problem, if anybody will need to generate 2d colliders for complex objects simply use this: https://www.h3xed.com/programming/automatically-create-polygon-collider-2d-from-2d-mesh-in-unity and make sure the geometry is "correct", i.e. it doesn't have any loose vertices.

    Thanks for help!
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,121
  11. ianian_

    ianian_

    Joined:
    Dec 28, 2017
    Posts:
    6
    Thanks @MelvMay! I wasn't aware of the option to draw filled colliders.
     
    MelvMay likes this.