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

Change Circle Collider 2D radius at runtime

Discussion in 'Project Tiny' started by abivos, Aug 6, 2021.

  1. abivos

    abivos

    Joined:
    Sep 15, 2018
    Posts:
    9
    Is there a way to change Circle Collider 2D radius at runtime?
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    If you look at Collider conversions for 2D Entities Physics you will see that all Colliders are converted into BlobAssets that will be referenced by PhysicsColliderBlob.
    To change the radius or any other property you must access the pointer to the collider then check if the collider is of the actual type you want and work from there.
    Try this:
    Code (CSharp):
    1.  
    2.     protected unsafe override void OnUpdate() {
    3.       Entities
    4.         .ForEach((Entity entity, in PhysicsColliderBlob physicsBlob) => {
    5.           var collider = physicsBlob.Collider.GetColliderPtr();
    6.           if (collider->ColliderType == ColliderType.Circle) {
    7.             ((PhysicsCircleCollider*)collider)->Geometry = new CircleGeometry {
    8.               Radius = 100f, // Your desired radius
    9.               Center = ((PhysicsCircleCollider*)collider)->Center
    10.             };
    11.           }
    12.         }).ScheduleParallel();
    13.     }
    14. }
     
    abivos likes this.
  3. abivos

    abivos

    Joined:
    Sep 15, 2018
    Posts:
    9
    Wow, thanks!