Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Why does CompositeCollider2D require a RigidBody2D?

Discussion in 'Physics' started by codinginflow, May 17, 2021.

  1. codinginflow

    codinginflow

    Joined:
    May 6, 2021
    Posts:
    30
    I could not find an answer to this question anywhere on Google. Why does a CompositeCollider2D automatically add a RigidBody2D whereas a Tilemap Collider does not need one?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    Because its scope has to be tied to any colliders attached to that body. It isn't a problem. Not having a Rigidbody2D is the same as having one and setting the body-type to Static (it's just that you're explicitly about it). Don't be fooled into thinking that not adding a Rigidbody2D somehow saves you something, it doesn't. It's just implicitly meaning add as a Static.

    A TilemapCollider2D is just another collider just like a CircleCollider2D or any other. In-fact a TilemapCollider2D IS tied to another component, the Tilemap.

    Various components require another component same as you can do for your scripts.
     
  3. codinginflow

    codinginflow

    Joined:
    May 6, 2021
    Posts:
    30
    I'm just trying to understand why it needs the Rigidbody. I don't want to get rid of it.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    Okay, I can ellaborate on what I mean by "its scope has to be tied to any colliders attached to that body".

    First, because the CompositeCollider2D is based upon a regular Collider2D, it HAS to be tied to a specific body (all colliders and their shapes are created against a physics body). If we were to allow it to be implicitly Static though (no Rigidbody2D specified) then, like any other Collider2D, it would be attached to the hidden Static ground-body that lives at the world origin. Unfortunately, this would mean that there's ambiguity when you select a Static colliders' "Used by Composite" check-box. It cannot easily figure out which composite that should be on because there can potentially be unlimited number of these against the Static ground-body. This in itself isn't a reason but it's a good part of it because having to searching hierarchies is expensive. Right now, when a Collider2D starts-up (created, loaded, modified), it has a directly access to the sole composite on a Rigidbody2D. In other words, when you add a CompositeCollider2D, it's registered against the Rigidbody2D. A collider using a composite only has to look at the attached body.



    Another important thing to understand is that the physics system at its fundamental level, really doesn't know about the Transform hierarchy which is a Unity thing. Searching hierarchies gets really expensive quickly. Not so much in moving to children or parents but in searching for components at those Transform levels. This is always avoided where possible so while this might not seem like much, as it scales, all this searching really adds up. It's certainly something we don't want to be doing on potentially lots of Collider2D using composites.

    Requiring a Rigidbody2D like this still allows you to be Static, Kinematic or Dynamic but makes it super obvious what the scope is: all Collider2D attached to the Rigidbody2D.

    It's a similar reason to why adding any Joint2D creates a Rigidbody2D; because it needs to be attached to it to limit the scope of what it affects.

    Side Note: There's a feature coming in 2021.2 that'll allow devs to make their own Collider2D including types like the CompositeCollider2D or TilemapCollider2D.

    Hope that helps.
     
  5. codinginflow

    codinginflow

    Joined:
    May 6, 2021
    Posts:
    30
    That makes a lot of sense, thank you very much for the explanation!
     
    MelvMay likes this.