Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Questions about tilemap physics

Discussion in '2D Experimental Preview' started by Iain-Lobb, Jun 3, 2016.

  1. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Sorry if this has been covered somewhere else, but how do you generate the Polygon collider in the Platforms demo? I can add new tiles to that layer but it doesn't update the collider.

    Question on capsule collider: I notice that this has the correct platformer behaviour in that it doesn't roll off the edge of platforms - in acts as if it was a box. But in what sense is it a capsule then?
     
    Deleted User likes this.
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Just add a Polygon collider to the tilemap layer and it should work. If you add more tiles, you can "Refresh collider" via the inspector and the collider includes the newly added tiles.

    [Edit] Sorry, it is Reset, not Refresh... access it via the triangle dropdown menu of the component.
     
  3. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    I don't seem to have a that option - I only have a Refesh All Tiles button on the Tile Map - no options on the collider
     
  4. Johaness_Reuben

    Johaness_Reuben

    Joined:
    Jan 27, 2016
    Posts:
    253
    upload_2016-6-3_22-45-28.png
    Yes, need to Reset from the dropdown marked red.

    Polygon Collider is just a temporary solution so you can do something with the tiles. Tilemap Collider is in the works.
     
  5. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Thanks, that worked :)
     
    der_r likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    I don't follow what you mean by roll off the edge of platforms. Are you sure you're not using it with the Z rotation constraint set on the Rigidbody2D which stops rotation? Beyond that, it'll rotate as normal. It's a capsule in the sense that this is its shape!
     
  7. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Sorry I'll explain: in a platform game, when you get to the edge of a platform, the expected behaviour is to fall off all at once. However, if you use a capsule for colider, the player would normally sink by a few pixel as they start to walk off the edge - as the outer part of the colider slides off
    The demo doesn't seem to have this problem, was wondering why not
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    I'm not sure who wrote the controller for it but it's setting the Rigidbody2D velocity directly when the character moves and stomping over the velocity applied by gravity in the Y axis which is why the character doesn't fall. The character can only move when it's consider grounded i.e. touching the floor.

    This has nothing to do with the CapsuleCollider2D and everything to do with how the character is being moved and how it determines if it's grounded or not.
     
  9. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Wondering what the use of the capsule collider is then if it has to be overridden to get platform game behavior?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    There's nothing being 'overridden', you misunderstand, this has nothing to do with the capsule collider but everything to do with how the Rigidbody2D is being driven. You could equally use a circle collider and the result would be the same

    I didn't write the controller and I have no idea why it has a capsule collider on it, it's using bad practice by setting velocity directly and the way it's detecting ground. It certainly isn't an example of using the capsule collider. I suspect it's an old controller script.

    Change the controller to move using Rigidbody2D.MovePosition and it'll be fine.

    Just try adding some Rigidbody2D with CapsuleCollider2D and they'll collide as you expect.
     
  11. Johaness_Reuben

    Johaness_Reuben

    Joined:
    Jan 27, 2016
    Posts:
    253
    As Melvyn said, its an old controller script. The character should be ignored, and is not a good example. I left it in there so the tile map example wouldn't be so boring :)
     
  12. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Hi guys - I'm coming at this with no prior knowledge of what is and isn't part of this release so forgive if I focus on the wrong things. A questions about 2D capsule collider - obviously having the option of one is great, but what is the intended use case for it?
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    It's typically used for characters as it's a convenient shape that tends to cover most character images. It also has no vertex corners that can catch when jumping/falling which can look odd and is often not wanted. Often devs will use a BoxCollider2D for the character body and a CircleCollider2D for the feet/base, sometimes another CircleCollider2D for the head/top. These separate colliders act as separate colliders resulting in the box collider still catching on other collider vertex when jumping/falling and still having three colliders to contend with.

    The CapsuleCollider2D has no vertex, is a single primitive collider and is very fast to compute as behind the scenes, it's just a single edge with a radius (capsule shape).

    It'll also smoothly roll-off surfaces as you suggest you wanted in your video.
     
  14. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    Classic 2D side-on games use a box collider for the character - the rolling off surfaces is actually a bad thing, not something I was saying we want. I can't really think of any examples of 2D games using a capsule collider but maybe there are some. The biggest issue I've found with doing platformers with unity's 2D physics is that characters want to slide down hills unless you write some extra code to apply an opposing force when on a slope. I definitely agree it's better to use forces than setting velocity directly, as the player can interact with other physics-driven objects properly.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    The CapsuleCollider2D has been asked for a lot for 2D platformers. Many platformers and examples do indeed use a circle shape for the feet although a lot of mechanics are controlled via physics queries and explicit movement using a Kinematic body and Rigidbody2D.MovePosition. The collider therefore providing a nice primitive that encapsules the character for col-det only. Many 2D style platformers written using 3D physics also use the (3D) CapsuleCollider.

    NOTE: The 'roll off platforms' actually revealed a bug. You will find that capsule/capsule contacts are fine with the capsule end-caps perfectly contacting however capsule/polygon or edge do indeed act like the end-caps are a box which shouldn't happen. Seems like something regressed when grafting this from our internal trunk branch.

    Not sure if we have a known problems section here on this forum but I'll look at getting that added.

    BTW: Note that if you go in 2D physics settings and open the Gizmo roll-out you will see an option of 'Show Collider Contacts' which draws little arrows where the contacts are. This can help visualize issues like this. For the above bug, the contact shows as a vertical line until the character falls which shouldn't happen.
     
  16. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    132
    I noticed that when I jump with the character into a wall, and keep moving into the wall, the character gets stuck on the side of the wall in the air. This happens both when using Capsule Collider 2D and Box Collider 2D on the character.

    Is this related to the bug you mentioned, or is it the old controller script's fault? Or is it because of the temporary physics solution?
     
  17. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    This is the character controller's fault. I'm rolling my own and it works perfectly. :)

    https://twitter.com/rendelmann/status/739716425300992000
     
    Sir-Spunky likes this.
  18. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I believe many "modern" platformers, like New Super Mario Bros. U and Donkey Kong Country Returns, are using capsule colliders. You can tell if a game is using a capsule collider by walking near an edge. If you drop down a bit before falling off... it's a capsule.
     
  19. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526