Search Unity

Best way to change Colliders size in runtime?

Discussion in 'Physics' started by litebox, Dec 27, 2021.

  1. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Hi. I have a gameplay field with a lots of CircleCollider2D objects (they are generated to fill almost all available space). At some point of time all objects change their size significantly (but their overall volume remains the same to be able to fit in the space).

    I have few ideas how to achieve this and would like to listen you suggestions about them:
    1. What is more efficient to do: change scale of game objects, which will update visual and physical parts of the object simultaneously or work with collider's radius + inner object (view) transform scale?
    2. I cannot update collider's size in one frame as it produces ton's of changes and all my objects jump around to fix collision's penetrations problems, so I think gradually update colliders to give Unity's physic simulation time to update all objects more naturally - the question is what will be better: use FixedUpdate to smoothly lerp collider's radiuses during one second, or use courotine with 3-5 iterations to update collider's size gradually?
    upload_2021-12-27_11-55-0.png

    Thank you for you help and Happy New Year!
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    If you can use 2021.2 onwards then the most efficient way is to use the CustomCollider2D which gives you direct access to all the primitive shapes. This can not only produce multiple different shape types in a single collider but it also allows you to change things very fast. It essentially allows you to create your own, fully functioning colliders and it can recreate all existing 2D Colliders we provide.

    You control the shapes by populating and changing a PhysicsShapeGroup2D.

    In your case you can use CustomCollider2D in replacement of CircleCollider2D. You can start with an empty shape group and Add a Circle Shape. Then you can assign a specific shape on it via the CustomCollider2D.SetCustomShape and change the shape (such as the radius) with PhysicsShapeGroup.SetShapeRadius.

    Whatever you do, don't ever modify the Transform when using 2D Physics; that's the role of the Rigidbody2D. Changing scale is the most inefficient way with changing the Radius property of the Circle being better and using a CustomCollider2D to get access to "the metal" being the most efficient.
     
    litebox likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I have a few dev videos showing how efficient it is like this one here:

     
    litebox likes this.
  4. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Wow, thank you for so detailed answer!
    You said "CustomCollider2D to get access to "the metal"" - By Metal do you mean Apple Hardware Acceleration will be used to calculate 2D Physic simulation? I'm developing mobile game, does it mean iOS devices with Metal support will use Hardware Acceleration for physic simulation as well? And what about Android devices?
    Cheers!
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    No, used as an expression which means go fast (a.k.a. "Pedal to the Metal") or straight to the core of the physics engine with little to no high-level Unity stuff in the way.

    There's no way you would get hardware acceleration just to change a shape though, there isn't any conceivable way that would help; changing a shape doesn't involve the simulation either. :)

    It's about modifying physics shapes only which the physics engine uses. Unity provides a bunch of colliders but they all only ever produce one of four different primitive physics shapes: Circle, Capsule, Polygon & Edge. There's not a single collider that can produce anything else. The CustomCollider2D allows you to use all these, in combination, as many as you like and gives you pretty much direct access to change them.

    So "Metal" = Raw Access. :)
     
  6. litebox

    litebox

    Joined:
    Aug 29, 2011
    Posts:
    158
    Ok, I see, thank you! :)