Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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:
    18
    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,626
    GroupIndex can often be used for this
     
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    @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.