Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Having issues with Tilemap Collider + Composite Collider

Discussion in '2D' started by bastupekka, Mar 24, 2023.

  1. bastupekka

    bastupekka

    Joined:
    Mar 24, 2023
    Posts:
    2
    Apologies if this is a beginner's issue, I just started out with Unity a couple of weeks ago.

    I keep having problems with player movement whenever I use the Tilemap Collider. The problem is that whenever the character moves to the right on the tilemap tiles, it is as if the player is constantly colliding with the tiles that I placed on the grid, causing a stuttering / jittering effect. This is probably due to the player jumping between different animations (my guess is that it switches between the walking and falling animation). The effect is quite subtle in the clip, but in my editor it basically looks as if the player is having a seizure.



    A couple of things to note are that this only occurs when the player moves to the right on tilemap tiles and the issue does not occur on "plain" game objects such as 2D squares (white platform above).

    Some things I've tried with no avail:
    - Replacing the player's box collider with a capsule collider (same result with a polygon collider).
    - Working with the standard tilemap collider without the composite collider. While this removed the jittering, it seemed to create spontaneous "invisible walls" that the player occasionally got stuck on (appeared infrequently, sometimes at the same spots, sometimes at different spots).

    The only workaround I have for the moment is to apply a box collider manually to the entire row of tiles. While I'm happy that it resolves the issue, it's not an optimal solution in the long run. Am I missing something obvious here?

    P.S. The player's rigidbody settings are set to Continous Collision Detection and Interpolate.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Never apologise for being a beginner. Everyone is at some point.

    I won't go too deep into the reasons but all physics engines suffer from these kinds of ghost collisions. It's mainly related to the fact that each of these "boxes" are different shapes and whilst you intend for them to be a continuous "top" surface to move over, the physics engine sees them as individual things and occasionally catches a corner/side so you get a physics response you don't want.

    The 2D physics engine provides edges which allow you to create larger-scale structures which do produce a single continuous surface by chaining edges.

    Two things provide this: the EdgeCollider2D and the CompositeCollider2D when using Outline geometry-type. The only reason you'd ever use a CompositeCollider2D on a TilemapCollider2D is to solve this issue because it has its drawbacks. If you are using a CompositeCollider2D but have left it in Polygon geometry-type then you're not getting advantage, only drawbacks.

    In short: Use Outline geometry-type (https://docs.unity3d.com/Manual/class-CompositeCollider2D.html).

    Hope this helps.
     
  3. bastupekka

    bastupekka

    Joined:
    Mar 24, 2023
    Posts:
    2
    Thank you so much for your quick reply and tips. I'll try it out! :)
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Good luck.
     
    bastupekka likes this.