Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Ignoring collision between specific entities

Discussion in 'Physics for ECS' started by CunningFox146, Oct 9, 2023.

  1. CunningFox146

    CunningFox146

    Joined:
    Mar 2, 2020
    Posts:
    24
    I need to ignore collision between several colliders, which are calculated during runtime. In MonoBehaviour we used Physics.IgnoreCollision method, and it gets the job done, but I failed to find this method for DOTS Physics. Are there any ways of replicating this behaviour in DOTS?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,797
    GroupIndex can often be used for this
     
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    330
    @CunningFox146 :
    You can use the
    CollisionFilter
    in your collider for this purpose, as @tertle pointed out.
    Have a look at its scripting API documentation.

    Every collider holds a collision filter (see Collider.Set/GetCollisionFilter).
    You can use the
    CollisionFilter.BelongsTo
    property to assign the given collider to different categories (or layers as in standard GameObjects).
    With the
    CollisionFilter.CollidesWith
    property you can enable or disable collision of this collider with different such categories (or layers).
    Both properties are bitmasks that you can modify using standard bit manipulation, e.g., setting the n'th bit in one of these properties using
    property |= 1 << n
    .

    Note that if the collision filter in one of two overlapping colliders dictates that it should not collide with the other, then the collision is disabled. In other words, the filters in both colliders need to indicate that a collision should occur for the overlapping collider pair to actually collide.

    Finally, the
    CollisionFilter.GroupIndex
    that @tertle pointed out above allows you to further group colliders and override their filter behavior altogether. If the
    GroupIndex
    is non-zero the following behavior is observed:
    • If in a pair of overlapping colliders, both colliders have the same positive
      GroupIndex
      value, they always collide.
    • Contrary, if in a pair of overlapping colliders, both colliders have the same negative
      GroupIndex
      value, they never collide.
    Let me know if this helps.
     
  4. zb737472783

    zb737472783

    Joined:
    Nov 8, 2018
    Posts:
    19
    Code (CSharp):
    1.  
    2. A.Value.SetCollisionFilter(new CollisionFilter
    3.                     {
    4.                         BelongsTo = (uint)CollisionFilterHelper.Test1,
    5.                         CollidesWith = (uint)CollisionFilterHelper.Test2
    6.                     };
    7. [/代码]
    8.  
    9. [code=CSharp]
    10. B.Value.SetCollisionFilter(new CollisionFilter
    11.                     {
    12.                         BelongsTo = (uint)CollisionFilterHelper.Test2,
    13.                         CollidesWith = (uint)CollisionFilterHelper.other
    14.                     };
    15. [/代码]
    16.  
    17. I would like to ask, the pseudo code is as above, Test1 is eager to collide with test2, and test2 is eager to collide with other. In this case, will test1 still collide with test2?
     
  5. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    330
    Have a look at the collision filter section in the official documentation here.
    I hope it will clarify the behaviour.

    For one, the
    BelongsTo
    and
    CollidesWith
    fields are bitfields and don't work as implied in your code snippet.
     
  6. zb737472783

    zb737472783

    Joined:
    Nov 8, 2018
    Posts:
    19
    The document is great, thank you
     
    daniel-holz likes this.